]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - sound/soc/bcm/pisound.c
Support for Blokas Labs pisound board
[mirror_ubuntu-zesty-kernel.git] / sound / soc / bcm / pisound.c
1 /*
2 * pisound Linux kernel module.
3 * Copyright (C) 2016 Vilniaus Blokas UAB, http://blokas.io/pisound
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2 of the
8 * License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
25 #include <linux/kobject.h>
26 #include <linux/sysfs.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/interrupt.h>
30 #include <linux/kfifo.h>
31
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/jack.h>
37 #include <sound/rawmidi.h>
38 #include <sound/asequencer.h>
39
40 static int pisnd_spi_init(struct device *dev);
41 static void pisnd_spi_uninit(void);
42
43 static void pisnd_spi_send(uint8_t val);
44 static uint8_t pisnd_spi_recv(uint8_t *buffer, uint8_t length);
45
46 typedef void (*pisnd_spi_recv_cb)(void *data);
47 static void pisnd_spi_set_callback(pisnd_spi_recv_cb cb, void *data);
48
49 static const char *pisnd_spi_get_serial(void);
50 static const char *pisnd_spi_get_id(void);
51 static const char *pisnd_spi_get_version(void);
52
53 static int pisnd_midi_init(struct snd_card *card);
54 static void pisnd_midi_uninit(void);
55
56 #define PISOUND_LOG_PREFIX "pisound: "
57
58 #ifdef DEBUG
59 # define printd(...) pr_alert(PISOUND_LOG_PREFIX __VA_ARGS__)
60 #else
61 # define printd(...) do {} while (0)
62 #endif
63
64 #define printe(...) pr_err(PISOUND_LOG_PREFIX __VA_ARGS__)
65 #define printi(...) pr_info(PISOUND_LOG_PREFIX __VA_ARGS__)
66
67 static int pisnd_output_open(struct snd_rawmidi_substream *substream)
68 {
69 return 0;
70 }
71
72 static int pisnd_output_close(struct snd_rawmidi_substream *substream)
73 {
74 return 0;
75 }
76
77 static void pisnd_output_trigger(
78 struct snd_rawmidi_substream *substream,
79 int up
80 )
81 {
82 uint8_t data;
83
84 if (!up)
85 return;
86
87 while (snd_rawmidi_transmit_peek(substream, &data, 1)) {
88 pisnd_spi_send(data);
89 snd_rawmidi_transmit_ack(substream, 1);
90 }
91 }
92
93 static void pisnd_output_drain(struct snd_rawmidi_substream *substream)
94 {
95 uint8_t data;
96
97 while (snd_rawmidi_transmit_peek(substream, &data, 1)) {
98 pisnd_spi_send(data);
99
100 snd_rawmidi_transmit_ack(substream, 1);
101 }
102 }
103
104 static int pisnd_input_open(struct snd_rawmidi_substream *substream)
105 {
106 return 0;
107 }
108
109 static int pisnd_input_close(struct snd_rawmidi_substream *substream)
110 {
111 return 0;
112 }
113
114 static void pisnd_midi_recv_callback(void *substream)
115 {
116 uint8_t data[128];
117 uint8_t n = 0;
118
119 while ((n = pisnd_spi_recv(data, sizeof(data)))) {
120 int res = snd_rawmidi_receive(substream, data, n);
121 (void)res;
122 printd("midi recv 0x%02x, res = %d\n", data, res);
123 }
124 }
125
126 static void pisnd_input_trigger(struct snd_rawmidi_substream *substream, int up)
127 {
128 if (up) {
129 pisnd_spi_set_callback(pisnd_midi_recv_callback, substream);
130 pisnd_midi_recv_callback(substream);
131 } else {
132 pisnd_spi_set_callback(NULL, NULL);
133 }
134 }
135
136 static struct snd_rawmidi *g_rmidi;
137
138 static struct snd_rawmidi_ops pisnd_output_ops = {
139 .open = pisnd_output_open,
140 .close = pisnd_output_close,
141 .trigger = pisnd_output_trigger,
142 .drain = pisnd_output_drain,
143 };
144
145 static struct snd_rawmidi_ops pisnd_input_ops = {
146 .open = pisnd_input_open,
147 .close = pisnd_input_close,
148 .trigger = pisnd_input_trigger,
149 };
150
151 static void pisnd_get_port_info(
152 struct snd_rawmidi *rmidi,
153 int number,
154 struct snd_seq_port_info *seq_port_info
155 )
156 {
157 seq_port_info->type =
158 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
159 SNDRV_SEQ_PORT_TYPE_HARDWARE |
160 SNDRV_SEQ_PORT_TYPE_PORT;
161 seq_port_info->midi_voices = 0;
162 }
163
164 static struct snd_rawmidi_global_ops pisnd_global_ops = {
165 .get_port_info = pisnd_get_port_info,
166 };
167
168 static int pisnd_midi_init(struct snd_card *card)
169 {
170 int err = snd_rawmidi_new(card, "pisound MIDI", 0, 1, 1, &g_rmidi);
171
172 if (err < 0) {
173 printe("snd_rawmidi_new failed: %d\n", err);
174 return err;
175 }
176
177 strcpy(g_rmidi->name, "pisound MIDI ");
178 strcat(g_rmidi->name, pisnd_spi_get_serial());
179
180 g_rmidi->info_flags =
181 SNDRV_RAWMIDI_INFO_OUTPUT |
182 SNDRV_RAWMIDI_INFO_INPUT |
183 SNDRV_RAWMIDI_INFO_DUPLEX;
184
185 g_rmidi->ops = &pisnd_global_ops;
186
187 g_rmidi->private_data = (void *)0;
188
189 snd_rawmidi_set_ops(
190 g_rmidi,
191 SNDRV_RAWMIDI_STREAM_OUTPUT,
192 &pisnd_output_ops
193 );
194
195 snd_rawmidi_set_ops(
196 g_rmidi,
197 SNDRV_RAWMIDI_STREAM_INPUT,
198 &pisnd_input_ops
199 );
200
201 return 0;
202 }
203
204 static void pisnd_midi_uninit(void)
205 {
206 }
207
208 static void *g_recvData;
209 static pisnd_spi_recv_cb g_recvCallback;
210
211 #define FIFO_SIZE 512
212
213 static char g_serial_num[11];
214 static char g_id[25];
215 static char g_version[5];
216
217 DEFINE_KFIFO(spi_fifo_in, uint8_t, FIFO_SIZE);
218 DEFINE_KFIFO(spi_fifo_out, uint8_t, FIFO_SIZE);
219
220 static struct gpio_desc *data_available;
221 static struct gpio_desc *spi_reset;
222
223 static struct spi_device *pisnd_spi_device;
224
225 static struct workqueue_struct *pisnd_workqueue;
226 static struct work_struct pisnd_work_process;
227
228 static void pisnd_work_handler(struct work_struct *work);
229
230 static uint16_t spi_transfer16(uint16_t val);
231
232 static int pisnd_init_workqueues(void)
233 {
234 pisnd_workqueue = create_singlethread_workqueue("pisnd_workqueue");
235 INIT_WORK(&pisnd_work_process, pisnd_work_handler);
236
237 return 0;
238 }
239
240 static void pisnd_uninit_workqueues(void)
241 {
242 flush_workqueue(pisnd_workqueue);
243 destroy_workqueue(pisnd_workqueue);
244
245 pisnd_workqueue = NULL;
246 }
247
248 static bool pisnd_spi_has_more(void)
249 {
250 return gpiod_get_value(data_available);
251 }
252
253 enum task_e {
254 TASK_PROCESS = 0,
255 };
256
257 static void pisnd_schedule_process(enum task_e task)
258 {
259 if (pisnd_spi_device != NULL &&
260 pisnd_workqueue != NULL &&
261 !work_pending(&pisnd_work_process)
262 ) {
263 printd("schedule: has more = %d\n", pisnd_spi_has_more());
264 if (task == TASK_PROCESS)
265 queue_work(pisnd_workqueue, &pisnd_work_process);
266 }
267 }
268
269 static irqreturn_t data_available_interrupt_handler(int irq, void *dev_id)
270 {
271 if (irq == gpiod_to_irq(data_available) && pisnd_spi_has_more()) {
272 printd("schedule from irq\n");
273 pisnd_schedule_process(TASK_PROCESS);
274 }
275
276 return IRQ_HANDLED;
277 }
278
279 static DEFINE_SPINLOCK(spilock);
280 static unsigned long spilockflags;
281
282 static uint16_t spi_transfer16(uint16_t val)
283 {
284 int err;
285 struct spi_transfer transfer;
286 struct spi_message msg;
287 uint8_t txbuf[2];
288 uint8_t rxbuf[2];
289
290 if (!pisnd_spi_device) {
291 printe("pisnd_spi_device null, returning\n");
292 return 0;
293 }
294
295 spi_message_init(&msg);
296
297 memset(&transfer, 0, sizeof(transfer));
298 memset(&rxbuf, 0, sizeof(rxbuf));
299
300 txbuf[0] = val >> 8;
301 txbuf[1] = val & 0xff;
302
303 transfer.tx_buf = &txbuf;
304 transfer.rx_buf = &rxbuf;
305 transfer.len = sizeof(txbuf);
306 transfer.speed_hz = 125000;
307 transfer.delay_usecs = 100;
308 spi_message_add_tail(&transfer, &msg);
309
310 spin_lock_irqsave(&spilock, spilockflags);
311 err = spi_sync(pisnd_spi_device, &msg);
312 spin_unlock_irqrestore(&spilock, spilockflags);
313
314 if (err < 0) {
315 printe("spi_sync error %d\n", err);
316 return 0;
317 }
318
319 printd("received: %02x%02x\n", rxbuf[0], rxbuf[1]);
320 printd("hasMore %d\n", pisnd_spi_has_more());
321
322 return (rxbuf[0] << 8) | rxbuf[1];
323 }
324
325 static int spi_read_bytes(char *dst, size_t length, uint8_t *bytesRead)
326 {
327 uint16_t rx;
328 uint8_t size;
329 uint8_t i;
330
331 memset(dst, 0, length);
332 *bytesRead = 0;
333
334 rx = spi_transfer16(0);
335 if (!(rx >> 8))
336 return -EINVAL;
337
338 size = rx & 0xff;
339
340 if (size > length)
341 return -EINVAL;
342
343 for (i = 0; i < size; ++i) {
344 rx = spi_transfer16(0);
345 if (!(rx >> 8))
346 return -EINVAL;
347
348 dst[i] = rx & 0xff;
349 }
350
351 *bytesRead = i;
352
353 return 0;
354 }
355
356 static int spi_device_match(struct device *dev, void *data)
357 {
358 struct spi_device *spi = container_of(dev, struct spi_device, dev);
359
360 printd(" %s %s %dkHz %d bits mode=0x%02X\n",
361 spi->modalias, dev_name(dev), spi->max_speed_hz/1000,
362 spi->bits_per_word, spi->mode);
363
364 if (strcmp("pisound-spi", spi->modalias) == 0) {
365 printi("\tFound!\n");
366 return 1;
367 }
368
369 printe("\tNot found!\n");
370 return 0;
371 }
372
373 static struct spi_device *pisnd_spi_find_device(void)
374 {
375 struct device *dev;
376
377 printi("Searching for spi device...\n");
378 dev = bus_find_device(&spi_bus_type, NULL, NULL, spi_device_match);
379 if (dev != NULL)
380 return container_of(dev, struct spi_device, dev);
381 else
382 return NULL;
383 }
384
385 static void pisnd_work_handler(struct work_struct *work)
386 {
387 uint16_t rx;
388 uint16_t tx;
389 uint8_t val;
390
391 if (work == &pisnd_work_process) {
392 if (pisnd_spi_device == NULL)
393 return;
394
395 do {
396 val = 0;
397 tx = 0;
398
399 if (kfifo_get(&spi_fifo_out, &val))
400 tx = 0x0f00 | val;
401
402 rx = spi_transfer16(tx);
403
404 if (rx & 0xff00) {
405 kfifo_put(&spi_fifo_in, rx & 0xff);
406 if (kfifo_len(&spi_fifo_in) > 16
407 && g_recvCallback)
408 g_recvCallback(g_recvData);
409 }
410 } while (rx != 0
411 || !kfifo_is_empty(&spi_fifo_out)
412 || pisnd_spi_has_more()
413 );
414
415 if (!kfifo_is_empty(&spi_fifo_in) && g_recvCallback)
416 g_recvCallback(g_recvData);
417 }
418 }
419
420 static int pisnd_spi_gpio_init(struct device *dev)
421 {
422 spi_reset = gpiod_get_index(dev, "reset", 1, GPIOD_ASIS);
423 data_available = gpiod_get_index(dev, "data_available", 0, GPIOD_ASIS);
424
425 gpiod_direction_output(spi_reset, 1);
426 gpiod_direction_input(data_available);
427
428 /* Reset the slave. */
429 gpiod_set_value(spi_reset, false);
430 mdelay(1);
431 gpiod_set_value(spi_reset, true);
432
433 /* Give time for spi slave to start. */
434 mdelay(64);
435
436 return 0;
437 }
438
439 static void pisnd_spi_gpio_uninit(void)
440 {
441 gpiod_set_value(spi_reset, false);
442 gpiod_put(spi_reset);
443 spi_reset = NULL;
444
445 gpiod_put(data_available);
446 data_available = NULL;
447 }
448
449 static int pisnd_spi_gpio_irq_init(struct device *dev)
450 {
451 return request_irq(
452 gpiod_to_irq(data_available),
453 data_available_interrupt_handler,
454 IRQF_TIMER | IRQF_TRIGGER_RISING,
455 "data_available_int",
456 NULL
457 );
458 }
459
460 static void pisnd_spi_gpio_irq_uninit(void)
461 {
462 free_irq(gpiod_to_irq(data_available), NULL);
463 }
464
465 static int spi_read_info(void)
466 {
467 uint16_t tmp;
468 uint8_t count;
469 uint8_t n;
470 uint8_t i;
471 uint8_t j;
472 char buffer[257];
473 int ret;
474 char *p;
475
476 memset(g_serial_num, 0, sizeof(g_serial_num));
477 memset(g_version, 0, sizeof(g_version));
478 memset(g_id, 0, sizeof(g_id));
479
480 tmp = spi_transfer16(0);
481
482 if (!(tmp >> 8))
483 return -EINVAL;
484
485 count = tmp & 0xff;
486
487 for (i = 0; i < count; ++i) {
488 memset(buffer, 0, sizeof(buffer));
489 ret = spi_read_bytes(buffer, sizeof(buffer)-1, &n);
490
491 if (ret < 0)
492 return ret;
493
494 switch (i) {
495 case 0:
496 if (n != 2)
497 return -EINVAL;
498
499 snprintf(
500 g_version,
501 sizeof(g_version),
502 "%x.%02x",
503 buffer[0],
504 buffer[1]
505 );
506 break;
507 case 1:
508 if (n >= sizeof(g_serial_num))
509 return -EINVAL;
510
511 memcpy(g_serial_num, buffer, sizeof(g_serial_num));
512 break;
513 case 2:
514 {
515 if (n >= sizeof(g_id))
516 return -EINVAL;
517
518 p = g_id;
519 for (j = 0; j < n; ++j)
520 p += sprintf(p, "%02x", buffer[j]);
521 }
522 break;
523 default:
524 break;
525 }
526 }
527
528 return 0;
529 }
530
531 static int pisnd_spi_init(struct device *dev)
532 {
533 int ret;
534 struct spi_device *spi;
535
536 memset(g_serial_num, 0, sizeof(g_serial_num));
537 memset(g_id, 0, sizeof(g_id));
538 memset(g_version, 0, sizeof(g_version));
539
540 spi = pisnd_spi_find_device();
541
542 if (spi != NULL) {
543 printd("initializing spi!\n");
544 pisnd_spi_device = spi;
545 ret = spi_setup(pisnd_spi_device);
546 } else {
547 printe("SPI device not found, deferring!\n");
548 return -EPROBE_DEFER;
549 }
550
551 ret = pisnd_spi_gpio_init(dev);
552
553 if (ret < 0) {
554 printe("SPI GPIO init failed: %d\n", ret);
555 spi_dev_put(pisnd_spi_device);
556 pisnd_spi_device = NULL;
557 pisnd_spi_gpio_uninit();
558 return ret;
559 }
560
561 ret = spi_read_info();
562
563 if (ret < 0) {
564 printe("Reading card info failed: %d\n", ret);
565 spi_dev_put(pisnd_spi_device);
566 pisnd_spi_device = NULL;
567 pisnd_spi_gpio_uninit();
568 return ret;
569 }
570
571 /* Flash the LEDs. */
572 spi_transfer16(0xf000);
573
574 ret = pisnd_spi_gpio_irq_init(dev);
575 if (ret < 0) {
576 printe("SPI irq request failed: %d\n", ret);
577 spi_dev_put(pisnd_spi_device);
578 pisnd_spi_device = NULL;
579 pisnd_spi_gpio_irq_uninit();
580 pisnd_spi_gpio_uninit();
581 }
582
583 ret = pisnd_init_workqueues();
584 if (ret != 0) {
585 printe("Workqueue initialization failed: %d\n", ret);
586 spi_dev_put(pisnd_spi_device);
587 pisnd_spi_device = NULL;
588 pisnd_spi_gpio_irq_uninit();
589 pisnd_spi_gpio_uninit();
590 pisnd_uninit_workqueues();
591 return ret;
592 }
593
594 if (pisnd_spi_has_more()) {
595 printd("data is available, scheduling from init\n");
596 pisnd_schedule_process(TASK_PROCESS);
597 }
598
599 return 0;
600 }
601
602 static void pisnd_spi_uninit(void)
603 {
604 pisnd_uninit_workqueues();
605
606 spi_dev_put(pisnd_spi_device);
607 pisnd_spi_device = NULL;
608
609 pisnd_spi_gpio_irq_uninit();
610 pisnd_spi_gpio_uninit();
611 }
612
613 static void pisnd_spi_send(uint8_t val)
614 {
615 kfifo_put(&spi_fifo_out, val);
616 printd("schedule from spi_send\n");
617 pisnd_schedule_process(TASK_PROCESS);
618 }
619
620 static uint8_t pisnd_spi_recv(uint8_t *buffer, uint8_t length)
621 {
622 return kfifo_out(&spi_fifo_in, buffer, length);
623 }
624
625 static void pisnd_spi_set_callback(pisnd_spi_recv_cb cb, void *data)
626 {
627 g_recvData = data;
628 g_recvCallback = cb;
629 }
630
631 static const char *pisnd_spi_get_serial(void)
632 {
633 if (strlen(g_serial_num))
634 return g_serial_num;
635
636 return "";
637 }
638
639 static const char *pisnd_spi_get_id(void)
640 {
641 if (strlen(g_id))
642 return g_id;
643
644 return "";
645 }
646
647 static const char *pisnd_spi_get_version(void)
648 {
649 if (strlen(g_version))
650 return g_version;
651
652 return "";
653 }
654
655 static const struct of_device_id pisound_of_match[] = {
656 { .compatible = "blokaslabs,pisound", },
657 { .compatible = "blokaslabs,pisound-spi", },
658 {},
659 };
660
661 static struct gpio_desc *osr0, *osr1, *osr2;
662 static struct gpio_desc *reset;
663 static struct gpio_desc *button;
664
665 static int pisnd_hw_params(
666 struct snd_pcm_substream *substream,
667 struct snd_pcm_hw_params *params
668 )
669 {
670 printd("rate = %d\n", params_rate(params));
671 printd("ch = %d\n", params_channels(params));
672 printd("bits = %u\n",
673 snd_pcm_format_physical_width(params_format(params)));
674 printd("format = %d\n", params_format(params));
675
676 gpiod_set_value(reset, false);
677
678 switch (params_rate(params)) {
679 case 48000:
680 gpiod_set_value(osr0, true);
681 gpiod_set_value(osr1, false);
682 gpiod_set_value(osr2, false);
683 break;
684 case 96000:
685 gpiod_set_value(osr0, true);
686 gpiod_set_value(osr1, true);
687 gpiod_set_value(osr2, false);
688 break;
689 case 192000:
690 gpiod_set_value(osr0, true);
691 gpiod_set_value(osr1, true);
692 gpiod_set_value(osr2, true);
693 break;
694 default:
695 printe("Unsupported rate %u!\n", params_rate(params));
696 return -EINVAL;
697 }
698
699 gpiod_set_value(reset, true);
700
701 return 0;
702 }
703
704 static unsigned int rates[3] = {
705 48000, 96000, 192000
706 };
707
708 static struct snd_pcm_hw_constraint_list constraints_rates = {
709 .count = ARRAY_SIZE(rates),
710 .list = rates,
711 .mask = 0,
712 };
713
714 static unsigned int sample_bits[] = {
715 24, 32
716 };
717
718 static struct snd_pcm_hw_constraint_list constraints_sample_bits = {
719 .count = ARRAY_SIZE(sample_bits),
720 .list = sample_bits,
721 .mask = 0,
722 };
723
724 static int pisnd_startup(struct snd_pcm_substream *substream)
725 {
726 int err = snd_pcm_hw_constraint_list(
727 substream->runtime,
728 0,
729 SNDRV_PCM_HW_PARAM_RATE,
730 &constraints_rates
731 );
732
733 if (err < 0)
734 return err;
735
736 err = snd_pcm_hw_constraint_list(
737 substream->runtime,
738 0,
739 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
740 &constraints_sample_bits
741 );
742
743 if (err < 0)
744 return err;
745
746 return 0;
747 }
748
749 static struct snd_soc_ops pisnd_ops = {
750 .startup = pisnd_startup,
751 .hw_params = pisnd_hw_params,
752 };
753
754 static struct snd_soc_dai_link pisnd_dai[] = {
755 {
756 .name = "pisound",
757 .stream_name = "pisound",
758 .cpu_dai_name = "bcm2708-i2s.0",
759 .codec_dai_name = "snd-soc-dummy-dai",
760 .platform_name = "bcm2708-i2s.0",
761 .codec_name = "snd-soc-dummy",
762 .dai_fmt =
763 SND_SOC_DAIFMT_I2S |
764 SND_SOC_DAIFMT_NB_NF |
765 SND_SOC_DAIFMT_CBM_CFM,
766 .ops = &pisnd_ops,
767 },
768 };
769
770 static int pisnd_card_probe(struct snd_soc_card *card)
771 {
772 int err = pisnd_midi_init(card->snd_card);
773
774 if (err < 0)
775 printe("pisnd_midi_init failed: %d\n", err);
776
777 return err;
778 }
779
780 static int pisnd_card_remove(struct snd_soc_card *card)
781 {
782 pisnd_midi_uninit();
783 return 0;
784 }
785
786 static struct snd_soc_card pisnd_card = {
787 .name = "pisound",
788 .owner = THIS_MODULE,
789 .dai_link = pisnd_dai,
790 .num_links = ARRAY_SIZE(pisnd_dai),
791 .probe = pisnd_card_probe,
792 .remove = pisnd_card_remove,
793 };
794
795 static int pisnd_init_gpio(struct device *dev)
796 {
797 osr0 = gpiod_get_index(dev, "osr", 0, GPIOD_ASIS);
798 osr1 = gpiod_get_index(dev, "osr", 1, GPIOD_ASIS);
799 osr2 = gpiod_get_index(dev, "osr", 2, GPIOD_ASIS);
800
801 reset = gpiod_get_index(dev, "reset", 0, GPIOD_ASIS);
802
803 button = gpiod_get_index(dev, "button", 0, GPIOD_ASIS);
804
805 gpiod_direction_output(osr0, 1);
806 gpiod_direction_output(osr1, 1);
807 gpiod_direction_output(osr2, 1);
808 gpiod_direction_output(reset, 1);
809
810 gpiod_set_value(reset, false);
811 gpiod_set_value(osr0, true);
812 gpiod_set_value(osr1, false);
813 gpiod_set_value(osr2, false);
814 gpiod_set_value(reset, true);
815
816 gpiod_export(button, false);
817
818 return 0;
819 }
820
821 static int pisnd_uninit_gpio(void)
822 {
823 int i;
824
825 struct gpio_desc **gpios[] = {
826 &osr0, &osr1, &osr2, &reset, &button,
827 };
828
829 gpiod_unexport(button);
830
831 for (i = 0; i < ARRAY_SIZE(gpios); ++i) {
832 if (*gpios[i] == NULL) {
833 printd("weird, GPIO[%d] is NULL already\n", i);
834 continue;
835 }
836
837 gpiod_put(*gpios[i]);
838 *gpios[i] = NULL;
839 }
840
841 return 0;
842 }
843
844 static struct kobject *pisnd_kobj;
845
846 static ssize_t pisnd_serial_show(
847 struct kobject *kobj,
848 struct kobj_attribute *attr,
849 char *buf
850 )
851 {
852 return sprintf(buf, "%s\n", pisnd_spi_get_serial());
853 }
854
855 static ssize_t pisnd_id_show(
856 struct kobject *kobj,
857 struct kobj_attribute *attr,
858 char *buf
859 )
860 {
861 return sprintf(buf, "%s\n", pisnd_spi_get_id());
862 }
863
864 static ssize_t pisnd_version_show(
865 struct kobject *kobj,
866 struct kobj_attribute *attr,
867 char *buf
868 )
869 {
870 return sprintf(buf, "%s\n", pisnd_spi_get_version());
871 }
872
873 static struct kobj_attribute pisnd_serial_attribute =
874 __ATTR(serial, 0644, pisnd_serial_show, NULL);
875 static struct kobj_attribute pisnd_id_attribute =
876 __ATTR(id, 0644, pisnd_id_show, NULL);
877 static struct kobj_attribute pisnd_version_attribute =
878 __ATTR(version, 0644, pisnd_version_show, NULL);
879
880 static struct attribute *attrs[] = {
881 &pisnd_serial_attribute.attr,
882 &pisnd_id_attribute.attr,
883 &pisnd_version_attribute.attr,
884 NULL
885 };
886
887 static struct attribute_group attr_group = { .attrs = attrs };
888
889 static int pisnd_probe(struct platform_device *pdev)
890 {
891 int ret = 0;
892 int i;
893
894 ret = pisnd_spi_init(&pdev->dev);
895 if (ret < 0) {
896 printe("pisnd_spi_init failed: %d\n", ret);
897 return ret;
898 }
899
900 printi("Detected pisound card:\n");
901 printi("\tSerial: %s\n", pisnd_spi_get_serial());
902 printi("\tVersion: %s\n", pisnd_spi_get_version());
903 printi("\tId: %s\n", pisnd_spi_get_id());
904
905 pisnd_kobj = kobject_create_and_add("pisound", kernel_kobj);
906 if (!pisnd_kobj) {
907 pisnd_spi_uninit();
908 return -ENOMEM;
909 }
910
911 ret = sysfs_create_group(pisnd_kobj, &attr_group);
912 if (ret < 0) {
913 pisnd_spi_uninit();
914 kobject_put(pisnd_kobj);
915 return -ENOMEM;
916 }
917
918 pisnd_init_gpio(&pdev->dev);
919 pisnd_card.dev = &pdev->dev;
920
921 if (pdev->dev.of_node) {
922 struct device_node *i2s_node;
923
924 i2s_node = of_parse_phandle(
925 pdev->dev.of_node,
926 "i2s-controller",
927 0
928 );
929
930 for (i = 0; i < pisnd_card.num_links; ++i) {
931 struct snd_soc_dai_link *dai = &pisnd_dai[i];
932
933 if (i2s_node) {
934 dai->cpu_dai_name = NULL;
935 dai->cpu_of_node = i2s_node;
936 dai->platform_name = NULL;
937 dai->platform_of_node = i2s_node;
938 dai->stream_name = pisnd_spi_get_serial();
939 }
940 }
941 }
942
943 ret = snd_soc_register_card(&pisnd_card);
944
945 if (ret < 0) {
946 printe("snd_soc_register_card() failed: %d\n", ret);
947 pisnd_uninit_gpio();
948 kobject_put(pisnd_kobj);
949 pisnd_spi_uninit();
950 }
951
952 return ret;
953 }
954
955 static int pisnd_remove(struct platform_device *pdev)
956 {
957 printi("Unloading.\n");
958
959 if (pisnd_kobj) {
960 kobject_put(pisnd_kobj);
961 pisnd_kobj = NULL;
962 }
963
964 pisnd_spi_uninit();
965
966 /* Turn off */
967 gpiod_set_value(reset, false);
968 pisnd_uninit_gpio();
969
970 return snd_soc_unregister_card(&pisnd_card);
971 }
972
973 MODULE_DEVICE_TABLE(of, pisound_of_match);
974
975 static struct platform_driver pisnd_driver = {
976 .driver = {
977 .name = "snd-rpi-pisound",
978 .owner = THIS_MODULE,
979 .of_match_table = pisound_of_match,
980 },
981 .probe = pisnd_probe,
982 .remove = pisnd_remove,
983 };
984
985 module_platform_driver(pisnd_driver);
986
987 MODULE_AUTHOR("Giedrius Trainavicius <giedrius@blokas.io>");
988 MODULE_DESCRIPTION("ASoC Driver for pisound, http://blokas.io/pisound");
989 MODULE_LICENSE("GPL v2");