]> Git Repo - linux.git/blob - drivers/staging/bcm2835-audio/bcm2835.c
thp: fix MADV_DONTNEED vs. MADV_FREE race
[linux.git] / drivers / staging / bcm2835-audio / bcm2835.c
1 /*****************************************************************************
2  * Copyright 2011 Broadcom Corporation.  All rights reserved.
3  *
4  * Unless you and Broadcom execute a separate written software license
5  * agreement governing use of this software, this software is licensed to you
6  * under the terms of the GNU General Public License version 2, available at
7  * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
8  *
9  * Notwithstanding the above, under no circumstances may you combine this
10  * software in any way with any other Broadcom software provided under a
11  * license other than the GPL, without Broadcom's express prior written
12  * consent.
13  *****************************************************************************/
14
15 #include <linux/platform_device.h>
16
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21
22 #include "bcm2835.h"
23
24 /* HACKY global pointers needed for successive probes to work : ssp
25  * But compared against the changes we will have to do in VC audio_ipc code
26  * to export 8 audio_ipc devices as a single IPC device and then monitor all
27  * four devices in a thread, this gets things done quickly and should be easier
28  * to debug if we run into issues
29  */
30
31 static struct snd_card *g_card;
32 static struct bcm2835_chip *g_chip;
33
34 static int snd_bcm2835_free(struct bcm2835_chip *chip)
35 {
36         kfree(chip);
37         return 0;
38 }
39
40 /* component-destructor
41  * (see "Management of Cards and Components")
42  */
43 static int snd_bcm2835_dev_free(struct snd_device *device)
44 {
45         return snd_bcm2835_free(device->device_data);
46 }
47
48 /* chip-specific constructor
49  * (see "Management of Cards and Components")
50  */
51 static int snd_bcm2835_create(struct snd_card *card,
52                               struct platform_device *pdev,
53                               struct bcm2835_chip **rchip)
54 {
55         struct bcm2835_chip *chip;
56         int err;
57         static struct snd_device_ops ops = {
58                 .dev_free = snd_bcm2835_dev_free,
59         };
60
61         *rchip = NULL;
62
63         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
64         if (!chip)
65                 return -ENOMEM;
66
67         chip->card = card;
68
69         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
70         if (err < 0) {
71                 snd_bcm2835_free(chip);
72                 return err;
73         }
74
75         *rchip = chip;
76         return 0;
77 }
78
79 static int snd_bcm2835_alsa_probe_dt(struct platform_device *pdev)
80 {
81         struct device *dev = &pdev->dev;
82         struct bcm2835_chip *chip;
83         struct snd_card *card;
84         u32 numchans;
85         int err, i;
86
87         err = of_property_read_u32(dev->of_node, "brcm,pwm-channels",
88                                    &numchans);
89         if (err) {
90                 dev_err(dev, "Failed to get DT property 'brcm,pwm-channels'");
91                 return err;
92         }
93
94         if (numchans == 0 || numchans > MAX_SUBSTREAMS) {
95                 numchans = MAX_SUBSTREAMS;
96                 dev_warn(dev, "Illegal 'brcm,pwm-channels' value, will use %u\n",
97                          numchans);
98         }
99
100         err = snd_card_new(&pdev->dev, -1, NULL, THIS_MODULE, 0, &card);
101         if (err) {
102                 dev_err(dev, "Failed to create soundcard structure\n");
103                 return err;
104         }
105
106         snd_card_set_dev(card, dev);
107         strcpy(card->driver, "bcm2835");
108         strcpy(card->shortname, "bcm2835 ALSA");
109         sprintf(card->longname, "%s", card->shortname);
110
111         err = snd_bcm2835_create(card, pdev, &chip);
112         if (err < 0) {
113                 dev_err(dev, "Failed to create bcm2835 chip\n");
114                 goto err_free;
115         }
116
117         err = snd_bcm2835_new_pcm(chip);
118         if (err < 0) {
119                 dev_err(dev, "Failed to create new bcm2835 pcm device\n");
120                 goto err_free;
121         }
122
123         err = snd_bcm2835_new_spdif_pcm(chip);
124         if (err < 0) {
125                 dev_err(dev, "Failed to create new bcm2835 spdif pcm device\n");
126                 goto err_free;
127         }
128
129         err = snd_bcm2835_new_ctl(chip);
130         if (err < 0) {
131                 dev_err(dev, "Failed to create new bcm2835 ctl\n");
132                 goto err_free;
133         }
134
135         for (i = 0; i < numchans; i++) {
136                 chip->avail_substreams |= (1 << i);
137                 chip->pdev[i] = pdev;
138         }
139
140         err = snd_card_register(card);
141         if (err) {
142                 dev_err(dev, "Failed to register bcm2835 ALSA card\n");
143                 goto err_free;
144         }
145
146         g_card = card;
147         g_chip = chip;
148         platform_set_drvdata(pdev, card);
149         audio_info("bcm2835 ALSA card created with %u channels\n", numchans);
150
151         return 0;
152
153 err_free:
154         snd_card_free(card);
155
156         return err;
157 }
158
159 static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
160 {
161         int idx;
162         void *drv_data;
163
164         drv_data = platform_get_drvdata(pdev);
165
166         if (drv_data == (void *)g_card) {
167                 /* This is the card device */
168                 snd_card_free((struct snd_card *)drv_data);
169                 g_card = NULL;
170                 g_chip = NULL;
171         } else {
172                 idx = (int)(long)drv_data;
173                 if (g_card) {
174                         BUG_ON(!g_chip);
175                         /* We pass chip device numbers in audio ipc devices
176                          * other than the one we registered our card with
177                          */
178                         idx = (int)(long)drv_data;
179                         BUG_ON(!idx || idx > MAX_SUBSTREAMS);
180                         g_chip->avail_substreams &= ~(1 << idx);
181                         /* There should be atleast one substream registered
182                          * after we are done here, as it wil be removed when
183                          * the *remove* is called for the card device
184                          */
185                         BUG_ON(!g_chip->avail_substreams);
186                 }
187         }
188
189         platform_set_drvdata(pdev, NULL);
190
191         return 0;
192 }
193
194 #ifdef CONFIG_PM
195
196 static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
197                                     pm_message_t state)
198 {
199         return 0;
200 }
201
202 static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
203 {
204         return 0;
205 }
206
207 #endif
208
209 static const struct of_device_id snd_bcm2835_of_match_table[] = {
210         { .compatible = "brcm,bcm2835-audio",},
211         {},
212 };
213 MODULE_DEVICE_TABLE(of, snd_bcm2835_of_match_table);
214
215 static struct platform_driver bcm2835_alsa0_driver = {
216         .probe = snd_bcm2835_alsa_probe_dt,
217         .remove = snd_bcm2835_alsa_remove,
218 #ifdef CONFIG_PM
219         .suspend = snd_bcm2835_alsa_suspend,
220         .resume = snd_bcm2835_alsa_resume,
221 #endif
222         .driver = {
223                 .name = "bcm2835_AUD0",
224                 .owner = THIS_MODULE,
225                 .of_match_table = snd_bcm2835_of_match_table,
226         },
227 };
228
229 static int bcm2835_alsa_device_init(void)
230 {
231         int retval;
232
233         retval = platform_driver_register(&bcm2835_alsa0_driver);
234         if (retval)
235                 pr_err("Error registering bcm2835_alsa0_driver %d .\n", retval);
236
237         return retval;
238 }
239
240 static void bcm2835_alsa_device_exit(void)
241 {
242         platform_driver_unregister(&bcm2835_alsa0_driver);
243 }
244
245 late_initcall(bcm2835_alsa_device_init);
246 module_exit(bcm2835_alsa_device_exit);
247
248 MODULE_AUTHOR("Dom Cobley");
249 MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
250 MODULE_LICENSE("GPL");
This page took 0.04848 seconds and 4 git commands to generate.