summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/simple-amplifier.c
blob: ca53b08c0b331ce8967398f321b3d07048677145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Support for gpio amplifier
 *   Copyright 2026 CS GROUP France
 *   Author: Herve Codina <herve.codina@bootlin.com>
 *
 * Basic simple amplifier driver
 *   Copyright (c) 2017 BayLibre, SAS.
 *   Author: Jerome Brunet <jbrunet@baylibre.com>
 */

#include <linux/bitmap.h>
#include <linux/bits.h>
#include <linux/gpio/consumer.h>
#include <linux/math.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <sound/soc.h>
#include <linux/sort.h>
#include <sound/tlv.h>

struct simple_amp_single {
	struct gpio_desc *gpio;
	bool is_inverted;
	int kctrl_val;
	const char *control_name;
};

struct simple_amp_point {
	u32 gpio_val;
	int gain_db;
};

struct simple_amp_range {
	unsigned int nb_points;
	struct simple_amp_point min;
	struct simple_amp_point max;
};

struct simple_amp_ranges {
	unsigned int nb_ranges;
	struct simple_amp_range *tab_ranges;
};

struct simple_amp_labels {
	unsigned int nb_labels;
	const char **tab_labels;
};

enum simple_amp_mode {
	SIMPLE_AMP_MODE_NONE,
	SIMPLE_AMP_MODE_RANGES,
	SIMPLE_AMP_MODE_LABELS,
};

struct simple_amp_multi {
	struct gpio_descs *gpios;
	u32 kctrl_val;
	u32 kctrl_max;
	const char *control_name;
	unsigned int *tlv_array;
	enum simple_amp_mode mode;
	union {
		struct simple_amp_ranges ranges;
		struct simple_amp_labels labels;
	};
};

struct simple_amp_data {
	unsigned int supports;
#define SIMPLE_AUDIO_SUPPORT_PGA		BIT(0)
#define SIMPLE_AUDIO_SUPPORT_POWER_SUPPLIES	BIT(1)
#define SIMPLE_AUDIO_SUPPORT_MUTE		BIT(2)
#define SIMPLE_AUDIO_SUPPORT_BYPASS		BIT(3)

	const struct snd_soc_dapm_widget *dapm_widgets;
	unsigned int num_dapm_widgets;
	const struct snd_soc_dapm_route *dapm_routes;
	unsigned int num_dapm_routes;
};

struct simple_amp {
	const struct simple_amp_data *data;
	struct gpio_desc *gpiod_enable;
	struct simple_amp_single mute;
	struct simple_amp_single bypass;
	struct simple_amp_multi gain;
};

static int simple_amp_power_event(struct snd_soc_dapm_widget *w,
				  struct snd_kcontrol *control, int event)
{
	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
	struct simple_amp *simple_amp = snd_soc_component_get_drvdata(c);
	int val;

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		val = 1;
		break;
	case SND_SOC_DAPM_PRE_PMD:
		val = 0;
		break;
	default:
		WARN(1, "Unexpected event");
		return -EINVAL;
	}

	gpiod_set_value_cansleep(simple_amp->gpiod_enable, val);

	return 0;
}

static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
	SND_SOC_DAPM_INPUT("INL"),
	SND_SOC_DAPM_INPUT("INR"),
	SND_SOC_DAPM_OUT_DRV_E("DRV", SND_SOC_NOPM, 0, 0, NULL, 0, simple_amp_power_event,
			       (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
	SND_SOC_DAPM_OUTPUT("OUTL"),
	SND_SOC_DAPM_OUTPUT("OUTR"),
	SND_SOC_DAPM_REGULATOR_SUPPLY("VCC", 20, 0),
};

static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
	{ "DRV", NULL, "INL" },
	{ "DRV", NULL, "INR" },
	{ "OUTL", NULL, "VCC" },
	{ "OUTR", NULL, "VCC" },
	{ "OUTL", NULL, "DRV" },
	{ "OUTR", NULL, "DRV" },
};

static const struct snd_soc_dapm_widget simple_amp_mono_pga_dapm_widgets[] = {
	SND_SOC_DAPM_INPUT("IN"),
	SND_SOC_DAPM_OUTPUT("OUT"),
	SND_SOC_DAPM_PGA_E("PGA", SND_SOC_NOPM, 0, 0, NULL, 0, simple_amp_power_event,
			   (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
	SND_SOC_DAPM_REGULATOR_SUPPLY("vdd", 0, 0),
};

static const struct snd_soc_dapm_route simple_amp_mono_pga_dapm_routes[] = {
	{ "PGA", NULL, "IN" },
	{ "PGA", NULL, "vdd" },
	{ "OUT", NULL, "PGA" },
};

static const struct snd_soc_dapm_widget simple_amp_stereo_pga_dapm_widgets[] = {
	SND_SOC_DAPM_INPUT("INL"),
	SND_SOC_DAPM_INPUT("INR"),
	SND_SOC_DAPM_OUTPUT("OUTL"),
	SND_SOC_DAPM_OUTPUT("OUTR"),
	SND_SOC_DAPM_PGA_E("PGA", SND_SOC_NOPM, 0, 0, NULL, 0, simple_amp_power_event,
			   (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
	SND_SOC_DAPM_REGULATOR_SUPPLY("vdd", 0, 0),
};

static const struct snd_soc_dapm_route simple_amp_stereo_pga_dapm_routes[] = {
	{ "PGA", NULL, "INL" },
	{ "PGA", NULL, "INR" },
	{ "PGA", NULL, "vdd" },
	{ "OUTL", NULL, "PGA" },
	{ "OUTR", NULL, "PGA" },
};

static int simple_amp_single_kctrl_write_gpio(struct simple_amp_single *single,
					      int kctrl_val)
{
	int gpio_val;

	gpio_val = single->is_inverted ? !kctrl_val : kctrl_val;

	return gpiod_set_value_cansleep(single->gpio, gpio_val);
}

static int simple_amp_single_kctrl_info(struct snd_kcontrol *kcontrol,
					struct snd_ctl_elem_info *uinfo)
{
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 1;
	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
	return 0;
}

static int simple_amp_single_kctrl_get(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_single *single = (struct simple_amp_single *)kcontrol->private_value;

	ucontrol->value.integer.value[0] = single->kctrl_val;

	return 0;
}

static int simple_amp_single_kctrl_put(struct snd_kcontrol *kcontrol,
				       struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_single *single = (struct simple_amp_single *)kcontrol->private_value;
	int kctrl_val;
	int err;

	kctrl_val = ucontrol->value.integer.value[0] ? 1 : 0;

	if (kctrl_val == single->kctrl_val)
		return 0;

	err = simple_amp_single_kctrl_write_gpio(single, kctrl_val);
	if (err)
		return err;

	single->kctrl_val = kctrl_val;

	return 1; /* The value changed */
}

static int simple_amp_single_add_kcontrol(struct snd_soc_component *component,
					  struct simple_amp_single *single)
{
	struct snd_kcontrol_new control = {
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = single->control_name,
		.info = simple_amp_single_kctrl_info,
		.get = simple_amp_single_kctrl_get,
		.put = simple_amp_single_kctrl_put,
		.private_value = (unsigned long)single,
	};
	int ret;

	/* Be consistent between single->kctrl_val value and the GPIO value */
	ret = simple_amp_single_kctrl_write_gpio(single, single->kctrl_val);
	if (ret)
		return ret;

	return snd_soc_add_component_controls(component, &control, 1);
}

static u32 simple_amp_multi_ranges_kctrl_to_gpio(u32 kctrl_val,
						 struct simple_amp_ranges *ranges)
{
	struct simple_amp_range *range;
	u32 index = kctrl_val;
	unsigned int i;

	for (i = 0; i < ranges->nb_ranges; i++) {
		range = &ranges->tab_ranges[i];

		if (index < range->nb_points)
			return (range->max.gpio_val >= range->min.gpio_val) ?
				range->min.gpio_val + index :
				range->min.gpio_val - index;

		index -= range->nb_points;
	}

	/*
	 * Given index out of possible ranges. This is shouldn't happen.
	 * Signal the issue and return the maximum value
	 */
	WARN(1, "kctrl_val %u out of ranges\n", kctrl_val);
	return ranges->tab_ranges[ranges->nb_ranges - 1].max.gpio_val;
}

static int simple_amp_multi_kctrl_write_gpios(struct simple_amp_multi *multi,
					      u32 kctrl_val)
{
	DECLARE_BITMAP(bm, 32);
	u32 gpio_val;

	if (kctrl_val > multi->kctrl_max)
		return -EINVAL;

	if (multi->mode == SIMPLE_AMP_MODE_RANGES)
		gpio_val = simple_amp_multi_ranges_kctrl_to_gpio(kctrl_val,
								 &multi->ranges);
	else
		gpio_val = kctrl_val;

	bitmap_from_arr32(bm, &gpio_val, multi->gpios->ndescs);

	return gpiod_multi_set_value_cansleep(multi->gpios, bm);
}

static int simple_amp_multi_kctrl_int_info(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_info *uinfo)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;

	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = multi->kctrl_max;
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	return 0;
}

static int simple_amp_multi_kctrl_int_get(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;

	ucontrol->value.integer.value[0] = multi->kctrl_val;
	return 0;
}

static int simple_amp_multi_kctrl_int_put(struct snd_kcontrol *kcontrol,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;
	u32 kctrl_val;
	int ret;

	kctrl_val = ucontrol->value.integer.value[0];

	if (kctrl_val == multi->kctrl_val)
		return 0;

	ret = simple_amp_multi_kctrl_write_gpios(multi, kctrl_val);
	if (ret)
		return ret;

	multi->kctrl_val = kctrl_val;

	return 1; /* The value changed */
}

static int simple_amp_multi_kctrl_enum_info(struct snd_kcontrol *kcontrol,
					    struct snd_ctl_elem_info *uinfo)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;

	return snd_ctl_enum_info(uinfo, 1, multi->labels.nb_labels,
				 multi->labels.tab_labels);
}

static int simple_amp_multi_kctrl_enum_get(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;

	ucontrol->value.enumerated.item[0] = multi->kctrl_val;
	return 0;
}

static int simple_amp_multi_kctrl_enum_put(struct snd_kcontrol *kcontrol,
					   struct snd_ctl_elem_value *ucontrol)
{
	struct simple_amp_multi *multi = (struct simple_amp_multi *)kcontrol->private_value;
	u32 kctrl_val;
	int ret;

	kctrl_val = ucontrol->value.enumerated.item[0];

	if (kctrl_val == multi->kctrl_val)
		return 0;

	ret = simple_amp_multi_kctrl_write_gpios(multi, kctrl_val);
	if (ret)
		return ret;

	multi->kctrl_val = kctrl_val;

	return 1; /* The value changed */
}

static unsigned int *simple_amp_alloc_tlv_ranges(const struct simple_amp_ranges *ranges)
{
	unsigned int index;
	unsigned int *tlv;
	unsigned int *t;
	unsigned int i;

	tlv = kzalloc_objs(*tlv, 2 + ranges->nb_ranges * 6, GFP_KERNEL);
	if (!tlv)
		return NULL;

	t = tlv;

	/* Fill first TLV */
	*t++ = SNDRV_CTL_TLVT_DB_RANGE; /* Tag */
	*t++ = ranges->nb_ranges * 6 * sizeof(*tlv); /* Len */
	/* Ranges are sorted from lower to higher value */
	index = 0;
	for (i = 0; i < ranges->nb_ranges; i++) {
		/* Fill range item i */
		*t++ = index;  /* min */
		index += ranges->tab_ranges[i].nb_points;
		*t++ = index - 1;  /* max */
		*t++ = SNDRV_CTL_TLVT_DB_MINMAX; /* Tag */
		*t++ = 2 * sizeof(*tlv); /* Len */
		*t++ = ranges->tab_ranges[i].min.gain_db; /* min_dB */
		*t++ = ranges->tab_ranges[i].max.gain_db; /* max_dB */
	}

	return tlv;
}

static int simple_amp_multi_add_kcontrol(struct snd_soc_component *component,
					 struct simple_amp_multi *multi)
{
	struct snd_kcontrol_new control = {
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = multi->control_name,
		.info = simple_amp_multi_kctrl_int_info,
		.get = simple_amp_multi_kctrl_int_get,
		.put = simple_amp_multi_kctrl_int_put,
		.private_value = (unsigned long)multi,
	};
	int ret;

	switch (multi->mode) {
	case SIMPLE_AMP_MODE_RANGES:
		multi->tlv_array = simple_amp_alloc_tlv_ranges(&multi->ranges);
		if (!multi->tlv_array)
			return -ENOMEM;

		control.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
				 SNDRV_CTL_ELEM_ACCESS_READWRITE;
		control.tlv.p = multi->tlv_array;
		break;

	case SIMPLE_AMP_MODE_LABELS:
		/* Use enumerated values */
		control.info = simple_amp_multi_kctrl_enum_info;
		control.get = simple_amp_multi_kctrl_enum_get;
		control.put = simple_amp_multi_kctrl_enum_put;
		break;

	case SIMPLE_AMP_MODE_NONE:
		/* Already set control configuration is enough */
		break;

	default:
		return -EINVAL;
	}

	/* Be consistent between multi->kctrl_val value and the GPIOs value */
	ret = simple_amp_multi_kctrl_write_gpios(multi, multi->kctrl_val);
	if (ret)
		goto err_free_tlv_array;

	ret = snd_soc_add_component_controls(component, &control, 1);
	if (ret)
		goto err_free_tlv_array;

	return 0;

err_free_tlv_array:
	kfree(multi->tlv_array);
	return ret;
}

static int simple_amp_add_basic_dapm(struct snd_soc_component *component)
{
	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
	struct simple_amp *simple_amp = snd_soc_component_get_drvdata(component);
	struct device *dev = component->dev;
	int ret;

	/* Add basic dapm widgets and routes */
	ret = snd_soc_dapm_new_controls(dapm, simple_amp->data->dapm_widgets,
					simple_amp->data->num_dapm_widgets);
	if (ret) {
		dev_err(dev, "Failed to add basic dapm widgets (%d)\n", ret);
		return ret;
	}

	ret = snd_soc_dapm_add_routes(dapm, simple_amp->data->dapm_routes,
				      simple_amp->data->num_dapm_routes);
	if (ret) {
		dev_err(dev, "Failed to add basic dapm routes (%d)\n", ret);
		return ret;
	}

	return 0;
}

struct simple_amp_supply {
	const char *prop_name;
	const struct snd_soc_dapm_widget dapm_widget;
	const struct snd_soc_dapm_route dapm_route;
};

static const struct simple_amp_supply simple_amp_supplies[] = {
	{
		.prop_name = "vddio-supply",
		.dapm_widget = SND_SOC_DAPM_REGULATOR_SUPPLY("vddio", 0, 0),
		.dapm_route = { "PGA", NULL, "vddio" },
	}, {
		.prop_name = "vdda1-supply",
		.dapm_widget = SND_SOC_DAPM_REGULATOR_SUPPLY("vdda1", 0, 0),
		.dapm_route = { "PGA", NULL, "vdda1" },
	}, {
		.prop_name = "vdda2-supply",
		.dapm_widget = SND_SOC_DAPM_REGULATOR_SUPPLY("vdda2", 0, 0),
		.dapm_route = { "PGA", NULL, "vdda2" },
	},
	{ /* End of list */}
};

static int simple_amp_add_power_supplies(struct snd_soc_component *component)
{
	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
	struct simple_amp *simple_amp = snd_soc_component_get_drvdata(component);
	const struct simple_amp_supply *supply;
	struct device *dev = component->dev;
	int ret;

	/*
	 * Those additional power supplies are attached to the PGA.
	 * If PGA is not supported, simply skipped them.
	 */
	if (!(simple_amp->data->supports & SIMPLE_AUDIO_SUPPORT_PGA)) {
		dev_err(dev, "Extra power supplied need PGA\n");
		return -EINVAL;
	}

	supply = simple_amp_supplies;
	do {
		if (!of_property_present(dev->of_node, supply->prop_name))
			continue;

		ret = snd_soc_dapm_new_controls(dapm, &supply->dapm_widget, 1);
		if (ret) {
			dev_err(dev, "Failed to add control for '%s' (%d)\n",
				supply->prop_name, ret);
			return ret;
		}
		ret = snd_soc_dapm_add_routes(dapm, &supply->dapm_route, 1);
		if (ret) {
			dev_err(dev, "Failed to add route for '%s' (%d)\n",
				supply->prop_name, ret);
			return ret;
		}
	} while ((++supply)->prop_name);

	return 0;
}

static int simple_amp_component_probe(struct snd_soc_component *component)
{
	struct simple_amp *simple_amp = snd_soc_component_get_drvdata(component);
	int ret;

	/* Add basic dapm widgets and routes */
	ret = simple_amp_add_basic_dapm(component);
	if (ret)
		return ret;

	/* Add additional power supplies */
	if (simple_amp->data->supports & SIMPLE_AUDIO_SUPPORT_POWER_SUPPLIES) {
		ret = simple_amp_add_power_supplies(component);
		if (ret)
			return ret;
	}

	if (simple_amp->mute.gpio) {
		/*
		 * The name of the GPIO used is mute. According to this name, 1
		 * means muted and 0 means un-muted.
		 *
		 * An inversion is expected by ALSA. Indeed from ALSA point of
		 * view, 1 means 'on' (un-muted) and 0 means 'off' (muted).
		 */
		simple_amp->mute.is_inverted = true;
		simple_amp->mute.kctrl_val = 1; /* Un-muted */
		ret = simple_amp_single_add_kcontrol(component, &simple_amp->mute);
		if (ret)
			return ret;
	}

	if (simple_amp->bypass.gpio) {
		ret = simple_amp_single_add_kcontrol(component, &simple_amp->bypass);
		if (ret)
			return ret;
	}

	if (simple_amp->gain.gpios) {
		ret = simple_amp_multi_add_kcontrol(component, &simple_amp->gain);
		if (ret)
			return ret;
	}

	return 0;
}

static void simple_amp_component_remove(struct snd_soc_component *component)
{
	struct simple_amp *simple_amp = snd_soc_component_get_drvdata(component);

	kfree(simple_amp->gain.tlv_array);
	simple_amp->gain.tlv_array = NULL;
}

static const struct snd_soc_component_driver simple_amp_component_driver = {
	.probe = simple_amp_component_probe,
	.remove = simple_amp_component_remove,
};

static int simple_amp_parse_single_gpio(struct device *dev,
					struct simple_amp_single *single,
					const char *gpio_property)
{
	/* Start with the inactive value */
	single->is_inverted = false;
	single->kctrl_val = 0;
	single->gpio = devm_gpiod_get_optional(dev, gpio_property, GPIOD_OUT_LOW);
	if (IS_ERR(single->gpio))
		return dev_err_probe(dev, PTR_ERR(single->gpio),
				     "Failed to get '%s' gpio\n",
				     gpio_property);
	return 0;
}

static int simple_amp_cmp_ranges(const void *a, const void *b)
{
	const struct simple_amp_range *a_range = a;
	const struct simple_amp_range *b_range = b;

	/* Ranges a and b don't overlap. This has been already checked */

	return a_range->min.gain_db - b_range->max.gain_db;
}

static int simple_amp_check_new_range(const struct simple_amp_range *new_range,
				      const struct simple_amp_range *tab_ranges,
				      unsigned int nb_ranges)
{
	unsigned int i;

	for (i = 0; i < nb_ranges; i++) {
		/* Check for range overlaps */
		if (new_range->min.gain_db >= tab_ranges[i].min.gain_db &&
		    new_range->min.gain_db <= tab_ranges[i].max.gain_db)
			return -EINVAL;

		if (new_range->max.gain_db >= tab_ranges[i].min.gain_db &&
		    new_range->max.gain_db <= tab_ranges[i].max.gain_db)
			return -EINVAL;

		if (new_range->min.gain_db <= tab_ranges[i].min.gain_db &&
		    new_range->max.gain_db >= tab_ranges[i].max.gain_db)
			return -EINVAL;
	}
	return 0;
}

static int simple_amp_parse_ranges(struct device *dev,
				   struct simple_amp_multi *multi,
				   const char *ranges_property)
{
	struct simple_amp_ranges *ranges = &multi->ranges;
	struct simple_amp_range *range;
	struct device_node *np = dev->of_node;
	struct simple_amp_point first_point;
	unsigned int max_gpio_val;
	unsigned int i;
	int ret;
	u32 u;
	s32 s;

	max_gpio_val = (1 << multi->gpios->ndescs) - 1;

	ret = of_property_count_u32_elems(np, ranges_property);
	if (ret < 0)
		return ret;

	/* The ranges array cannot be empty */
	if (ret == 0)
		return -EINVAL;
	/*
	 * One range item is composed of 2 points and each point is composed of
	 * 2 values.
	 */
	if (ret % 4)
		return -EINVAL;

	ranges->nb_ranges = ret / 4;

	/* The worst case is one range per possible gpio value */
	if (ranges->nb_ranges > max_gpio_val + 1)
		return -EINVAL;

	ranges->tab_ranges = devm_kcalloc(dev, ranges->nb_ranges,
					  sizeof(*ranges->tab_ranges),
					  GFP_KERNEL);
	if (!ranges->tab_ranges)
		return -ENOMEM;

	multi->kctrl_max = 0;
	for (i = 0; i < ranges->nb_ranges; i++) {
		range = &ranges->tab_ranges[i];

		/* First gpios value */
		ret = of_property_read_u32_index(np, ranges_property, i * 4, &u);
		if (ret)
			return ret;
		if (u > max_gpio_val)
			return -EINVAL;

		range->min.gpio_val = u;

		/* First Gain value */
		ret = of_property_read_s32_index(np, ranges_property, i * 4 + 1, &s);
		if (ret)
			return ret;

		range->min.gain_db = s;

		/* Second gpios value */
		ret = of_property_read_u32_index(np, ranges_property, i * 4 + 2, &u);
		if (ret)
			return ret;
		if (u > max_gpio_val)
			return -EINVAL;

		range->max.gpio_val = u;

		/* Second Gain value */
		ret = of_property_read_s32_index(np, ranges_property, i * 4 + 3, &s);
		if (ret)
			return ret;

		range->max.gain_db = s;

		/* Save the first point for later usage */
		if (i == 0)
			first_point = range->min;

		/* Fix min and max if needed */
		if (range->min.gain_db > range->max.gain_db)
			swap(range->min, range->max);

		ret = simple_amp_check_new_range(range, ranges->tab_ranges, i);
		if (ret)
			return ret;

		range->nb_points = abs_diff(range->min.gpio_val,
					    range->max.gpio_val) + 1;

		multi->kctrl_max += range->nb_points;
	}

	multi->kctrl_max -= 1;

	/* Sort the tab_range array by gain_db value */
	sort(ranges->tab_ranges, ranges->nb_ranges, sizeof(*ranges->tab_ranges),
	     simple_amp_cmp_ranges, NULL);

	/*
	 * multi->kctrl_val is the index in tab_ranges.
	 *
	 * Choose to have the initial amplification value set to the first point
	 * available in the first range available in the tab_ranges array before
	 * sorting.
	 *
	 * This first point has been identified before sorting. Search for it in
	 * the sorted array in order to set the multi->kctrl_val initial value.
	 */
	multi->kctrl_val = 0;
	for (i = 0; i < ranges->nb_ranges; i++) {
		range = &ranges->tab_ranges[i];

		if (range->min.gpio_val == first_point.gpio_val &&
		    range->min.gain_db == first_point.gain_db)
			break;

		multi->kctrl_val += range->nb_points;

		if (range->max.gpio_val == first_point.gpio_val &&
		    range->max.gain_db == first_point.gain_db) {
			multi->kctrl_val--;
			break;
		}
	}

	return 0;
}

static int simple_amp_parse_labels(struct device *dev,
				   struct simple_amp_multi *multi,
				   const char *labels_property)
{
	struct simple_amp_labels *labels = &multi->labels;
	struct device_node *np = dev->of_node;
	int ret;

	ret = of_property_count_strings(np, labels_property);
	if (ret < 0)
		return ret;

	/* The labels array cannot be empty */
	if (ret == 0)
		return -EINVAL;

	labels->nb_labels = ret;
	if (labels->nb_labels > (1 << multi->gpios->ndescs))
		return -EINVAL;

	labels->tab_labels = devm_kcalloc(dev, labels->nb_labels,
					  sizeof(*labels->tab_labels),
					  GFP_KERNEL);
	if (!labels->tab_labels)
		return -ENOMEM;

	multi->kctrl_max = labels->nb_labels - 1;
	multi->kctrl_val = 0;

	return of_property_read_string_array(np, labels_property, labels->tab_labels,
					     labels->nb_labels);
}

static int simple_amp_parse_multi_gpio(struct device *dev,
				       struct simple_amp_multi *multi,
				       const char *gpios_property,
				       const char *ranges_property,
				       const char *labels_property)
{
	struct device_node *np = dev->of_node;
	int ret;

	/* Start with the value 0 (GPIO inactive). Can be changed later */
	multi->kctrl_val = 0;
	multi->gpios = devm_gpiod_get_array_optional(dev, gpios_property, GPIOD_OUT_LOW);
	if (IS_ERR(multi->gpios))
		return dev_err_probe(dev, PTR_ERR(multi->gpios),
				     "Failed to get '%s' gpios\n",
				     gpios_property);
	if (!multi->gpios)
		return 0;

	if (multi->gpios->ndescs > 16)
		return dev_err_probe(dev, -EINVAL,
				     "Number of '%s' gpios limited to 16\n",
				     gpios_property);

	/* Set default value for the kctrl_max. Can be changed later */
	multi->kctrl_max = (1 << multi->gpios->ndescs) - 1;

	multi->mode = SIMPLE_AMP_MODE_NONE;
	if (of_property_present(np, ranges_property)) {
		ret = simple_amp_parse_ranges(dev, multi, ranges_property);
		if (ret < 0)
			return dev_err_probe(dev, ret, "Failed to parse '%s'\n",
					     ranges_property);
		multi->mode = SIMPLE_AMP_MODE_RANGES;
	} else if (of_property_present(np, labels_property)) {
		ret = simple_amp_parse_labels(dev, multi, labels_property);
		if (ret < 0)
			return dev_err_probe(dev, ret, "Failed to parse '%s'\n",
					     labels_property);

		multi->mode = SIMPLE_AMP_MODE_LABELS;
	}

	return 0;
}

static int simple_amp_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct simple_amp *simple_amp;
	int ret;

	simple_amp = devm_kzalloc(dev, sizeof(*simple_amp), GFP_KERNEL);
	if (!simple_amp)
		return -ENOMEM;
	platform_set_drvdata(pdev, simple_amp);

	simple_amp->data = of_device_get_match_data(dev);
	if (!simple_amp->data)
		return -EINVAL;

	simple_amp->gpiod_enable = devm_gpiod_get_optional(dev, "enable",
							   GPIOD_OUT_LOW);
	if (IS_ERR(simple_amp->gpiod_enable))
		return dev_err_probe(dev, PTR_ERR(simple_amp->gpiod_enable),
				     "Failed to get 'enable' gpio");

	if (simple_amp->data->supports & SIMPLE_AUDIO_SUPPORT_MUTE) {
		ret = simple_amp_parse_single_gpio(dev, &simple_amp->mute, "mute");
		if (ret)
			return ret;
	}

	if (simple_amp->data->supports & SIMPLE_AUDIO_SUPPORT_BYPASS) {
		ret = simple_amp_parse_single_gpio(dev, &simple_amp->bypass, "bypass");
		if (ret)
			return ret;
	}

	if (simple_amp->data->supports & SIMPLE_AUDIO_SUPPORT_PGA) {
		ret = simple_amp_parse_multi_gpio(dev, &simple_amp->gain, "gain",
						  "gain-ranges", "gain-labels");
		if (ret)
			return ret;
	}

	/* Set controls name */
	simple_amp->gain.control_name = "Volume";
	simple_amp->mute.control_name = "Switch";
	simple_amp->bypass.control_name = "Bypass Switch";

	if (simple_amp->gain.mode == SIMPLE_AMP_MODE_LABELS) {
		/*
		 * The gain widget control will use enumerated values.
		 *
		 * Having just "Voltage" and "Switch" widget names with
		 * enumerated values and boolean value can confuse ALSA in terms
		 * of possible values (strings).
		 *
		 * Make things clear and avoid the just "Switch" name in that
		 * case.
		 */
		simple_amp->mute.control_name = "Out Switch";
	}

	return devm_snd_soc_register_component(dev,
					       &simple_amp_component_driver,
					       NULL, 0);
}

static const struct simple_amp_data simple_audio_amplifier_data = {
	.dapm_widgets		= simple_amp_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(simple_amp_dapm_widgets),
	.dapm_routes		= simple_amp_dapm_routes,
	.num_dapm_routes	= ARRAY_SIZE(simple_amp_dapm_routes),
};

static const struct simple_amp_data simple_audio_mono_pga_data = {
	.supports		= SIMPLE_AUDIO_SUPPORT_PGA |
				  SIMPLE_AUDIO_SUPPORT_POWER_SUPPLIES |
				  SIMPLE_AUDIO_SUPPORT_MUTE |
				  SIMPLE_AUDIO_SUPPORT_BYPASS,
	.dapm_widgets		= simple_amp_mono_pga_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(simple_amp_mono_pga_dapm_widgets),
	.dapm_routes		= simple_amp_mono_pga_dapm_routes,
	.num_dapm_routes	= ARRAY_SIZE(simple_amp_mono_pga_dapm_routes),
};

static const struct simple_amp_data simple_audio_stereo_pga_data = {
	.supports		= SIMPLE_AUDIO_SUPPORT_PGA |
				  SIMPLE_AUDIO_SUPPORT_POWER_SUPPLIES |
				  SIMPLE_AUDIO_SUPPORT_MUTE |
				  SIMPLE_AUDIO_SUPPORT_BYPASS,
	.dapm_widgets		= simple_amp_stereo_pga_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(simple_amp_stereo_pga_dapm_widgets),
	.dapm_routes		= simple_amp_stereo_pga_dapm_routes,
	.num_dapm_routes	= ARRAY_SIZE(simple_amp_stereo_pga_dapm_routes),
};

static const struct of_device_id simple_amp_ids[] = {
	{ .compatible = "dioo,dio2125",		  .data = &simple_audio_amplifier_data},
	{ .compatible = "simple-audio-amplifier", .data = &simple_audio_amplifier_data},
	{ .compatible = "gpio-audio-amp-mono",	  .data = &simple_audio_mono_pga_data},
	{ .compatible = "gpio-audio-amp-stereo",  .data = &simple_audio_stereo_pga_data},
	{ }
};
MODULE_DEVICE_TABLE(of, simple_amp_ids);

static struct platform_driver simple_amp_driver = {
	.driver = {
		.name = "simple-amplifier",
		.of_match_table = simple_amp_ids,
	},
	.probe = simple_amp_probe,
};

module_platform_driver(simple_amp_driver);

MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
MODULE_LICENSE("GPL");