~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to sound/soc/omap/abe/port_mgr.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ALSA SoC OMAP ABE port manager
 
3
 *
 
4
 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * version 2 as published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
 * 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 St, Fifth Floor, Boston, MA
 
18
 * 02110-1301 USA
 
19
 */
 
20
 
 
21
//#define DEBUG
 
22
 
 
23
#include <linux/module.h>
 
24
#include <linux/init.h>
 
25
#include <linux/slab.h>
 
26
#include <linux/errno.h>
 
27
#include <linux/spinlock.h>
 
28
#include <linux/list.h>
 
29
#include <linux/debugfs.h>
 
30
#include <linux/device.h>
 
31
#include "port_mgr.h"
 
32
#include "abe_main.h"
 
33
 
 
34
#ifdef CONFIG_DEBUG_FS
 
35
/* this must match logical ID numbers in port_mgr.h */
 
36
static const char *lport_name[] = {
 
37
                "dmic0", "dmic1", "dmic2", "pdmdl1", "pdmdl2", "pdmvib",
 
38
                "pdmul1", "bt_vx_dl", "bt_vx_ul", "mm_ext_ul", "mm_ext_dl",
 
39
                "mm_dl1", "mm_ul1", "mm_ul2", "vx_dl", "vx_ul", "vib", "tones"
 
40
};
 
41
#endif
 
42
 
 
43
static DEFINE_MUTEX(port_mgr_mutex);
 
44
static struct abe *the_abe = NULL;
 
45
static int users = 0;
 
46
 
 
47
/*
 
48
 * Get the Physical port ID based on the logical port ID
 
49
 *
 
50
 * FE and BE ports have unique ID's within the driver but share
 
51
 * ID's within the ABE. This maps a driver port ID to an ABE port ID.
 
52
 */
 
53
static int get_physical_id(int logical_id)
 
54
{
 
55
        switch (logical_id) {
 
56
        /* backend ports */
 
57
        case OMAP_ABE_BE_PORT_DMIC0:
 
58
        case OMAP_ABE_BE_PORT_DMIC1:
 
59
        case OMAP_ABE_BE_PORT_DMIC2:
 
60
                return DMIC_PORT;
 
61
        case OMAP_ABE_BE_PORT_PDM_DL1:
 
62
        case OMAP_ABE_BE_PORT_PDM_DL2:
 
63
                return PDM_DL_PORT;
 
64
        case OMAP_ABE_BE_PORT_PDM_VIB:
 
65
                return VIB_DL_PORT;
 
66
        case OMAP_ABE_BE_PORT_PDM_UL1:
 
67
                return PDM_UL_PORT;
 
68
        case OMAP_ABE_BE_PORT_BT_VX_DL:
 
69
                return BT_VX_DL_PORT;
 
70
        case OMAP_ABE_BE_PORT_BT_VX_UL:
 
71
                return BT_VX_UL_PORT;
 
72
        case OMAP_ABE_BE_PORT_MM_EXT_UL:
 
73
                return MM_EXT_OUT_PORT;
 
74
        case OMAP_ABE_BE_PORT_MM_EXT_DL:
 
75
                return MM_EXT_IN_PORT;
 
76
        /* front end ports */
 
77
        case OMAP_ABE_FE_PORT_MM_DL1:
 
78
                return MM_DL_PORT;
 
79
        case OMAP_ABE_FE_PORT_MM_UL1:
 
80
                return MM_UL_PORT;
 
81
        case OMAP_ABE_FE_PORT_MM_UL2:
 
82
                return MM_UL2_PORT;
 
83
        case OMAP_ABE_FE_PORT_VX_DL:
 
84
                return VX_DL_PORT;
 
85
        case OMAP_ABE_FE_PORT_VX_UL:
 
86
                return VX_UL_PORT;
 
87
        case OMAP_ABE_FE_PORT_VIB:
 
88
                return VIB_DL_PORT;
 
89
        case OMAP_ABE_FE_PORT_TONES:
 
90
                return TONES_DL_PORT;
 
91
        }
 
92
        return -EINVAL;
 
93
}
 
94
 
 
95
/*
 
96
 * Get the number of enabled users of the physical port shared by this client.
 
97
 * Locks held by callers.
 
98
 */
 
99
static int port_get_num_users(struct abe *abe, struct omap_abe_port *port)
 
100
{
 
101
        struct omap_abe_port *p;
 
102
        int users = 0;
 
103
 
 
104
        list_for_each_entry(p, &abe->ports, list) {
 
105
                if (p->physical_id == port->physical_id && p->state == PORT_ENABLED)
 
106
                        users++;
 
107
        }
 
108
        return users;
 
109
}
 
110
 
 
111
static int port_is_open(struct abe *abe, int phy_port)
 
112
{
 
113
        struct omap_abe_port *p;
 
114
 
 
115
        list_for_each_entry(p, &abe->ports, list) {
 
116
                if (p->physical_id == phy_port && p->state == PORT_ENABLED)
 
117
                        return 1;
 
118
        }
 
119
        return 0;
 
120
}
 
121
 
 
122
/*
 
123
 * Check whether the physical port is enabled for this PHY port ID.
 
124
 * Locks held by callers.
 
125
 */
 
126
int omap_abe_port_is_enabled(struct abe *abe, struct omap_abe_port *port)
 
127
{
 
128
        struct omap_abe_port *p;
 
129
        unsigned long flags;
 
130
 
 
131
        spin_lock_irqsave(&abe->lock, flags);
 
132
 
 
133
        list_for_each_entry(p, &abe->ports, list) {
 
134
                if (p->physical_id == port->physical_id && p->state == PORT_ENABLED) {
 
135
                        spin_unlock_irqrestore(&abe->lock, flags);
 
136
                        return 1;
 
137
                }
 
138
        }
 
139
 
 
140
        spin_unlock_irqrestore(&abe->lock, flags);
 
141
        return 0;
 
142
}
 
143
EXPORT_SYMBOL(omap_abe_port_is_enabled);
 
144
 
 
145
/*
 
146
 * omap_abe_port_enable - enable ABE logical port
 
147
 *
 
148
 * @abe -  ABE.
 
149
 * @port - logical ABE port ID to be enabled.
 
150
 */
 
151
int omap_abe_port_enable(struct abe *abe, struct omap_abe_port *port)
 
152
{
 
153
        int ret = 0;
 
154
        unsigned long flags;
 
155
 
 
156
        /* only enable the physical port iff it is disabled */
 
157
        pr_debug("port %s increment count %d\n",
 
158
                        lport_name[port->logical_id], port->users);
 
159
 
 
160
        spin_lock_irqsave(&abe->lock, flags);
 
161
        if (port->users == 0 && port_get_num_users(abe, port) == 0) {
 
162
 
 
163
                /* enable the physical port */
 
164
                pr_debug("port %s phy port %d enabled\n",
 
165
                        lport_name[port->logical_id], port->physical_id);
 
166
                abe_enable_data_transfer(port->physical_id);
 
167
        }
 
168
 
 
169
        port->state = PORT_ENABLED;
 
170
        port->users++;
 
171
        spin_unlock_irqrestore(&abe->lock, flags);
 
172
        return ret;
 
173
}
 
174
EXPORT_SYMBOL(omap_abe_port_enable);
 
175
 
 
176
/*
 
177
 * omap_abe_port_disable - disable ABE logical port
 
178
 *
 
179
 * @abe -  ABE.
 
180
 * @port - logical ABE port ID to be disabled.
 
181
 */
 
182
int omap_abe_port_disable(struct abe *abe, struct omap_abe_port *port)
 
183
{
 
184
        int ret = 0;
 
185
        unsigned long flags;
 
186
 
 
187
        /* only disable the port iff no other users are using it */
 
188
        pr_debug("port %s decrement count %d\n",
 
189
                        lport_name[port->logical_id], port->users);
 
190
 
 
191
        spin_lock_irqsave(&abe->lock, flags);
 
192
        if (port->users == 1 && port_get_num_users(abe, port) == 1) {
 
193
                /* disable the physical port */
 
194
                pr_debug("port %s phy port %d disabled\n",
 
195
                        lport_name[port->logical_id], port->physical_id);
 
196
 
 
197
                abe_disable_data_transfer(port->physical_id);
 
198
        }
 
199
 
 
200
        port->state = PORT_DISABLED;
 
201
        port->users--;
 
202
        spin_unlock_irqrestore(&abe->lock, flags);
 
203
        return ret;
 
204
}
 
205
EXPORT_SYMBOL(omap_abe_port_disable);
 
206
 
 
207
/*
 
208
 * omap_abe_port_open - open ABE logical port
 
209
 *
 
210
 * @abe -  ABE.
 
211
 * @logical_id - logical ABE port ID to be opened.
 
212
 */
 
213
struct omap_abe_port *omap_abe_port_open(struct abe *abe, int logical_id)
 
214
{
 
215
        struct omap_abe_port *port;
 
216
        unsigned long flags;
 
217
 
 
218
#ifdef CONFIG_DEBUG_FS
 
219
        char debug_fs_name[32];
 
220
#endif
 
221
 
 
222
        if (logical_id < 0 || logical_id > OMAP_ABE_MAX_PORT_ID)
 
223
                return NULL;
 
224
 
 
225
        if (port_is_open(abe, logical_id))
 
226
                return NULL;
 
227
 
 
228
        port = kzalloc(sizeof(struct omap_abe_port), GFP_KERNEL);
 
229
        if (port == NULL)
 
230
                return NULL;
 
231
 
 
232
        port->logical_id = logical_id;
 
233
        port->physical_id = get_physical_id(logical_id);
 
234
        port->state = PORT_DISABLED;
 
235
        port->abe = abe;
 
236
 
 
237
        spin_lock_irqsave(&abe->lock, flags);
 
238
        list_add(&port->list, &abe->ports);
 
239
        spin_unlock_irqrestore(&abe->lock, flags);
 
240
        port->physical_users = port_get_num_users(abe, port);
 
241
 
 
242
#ifdef CONFIG_DEBUG_FS
 
243
        sprintf(debug_fs_name, "%s_state", lport_name[logical_id]);
 
244
        port->debugfs_lstate = debugfs_create_u32(debug_fs_name, 0644,
 
245
                        abe->debugfs_root, &port->state);
 
246
        sprintf(debug_fs_name, "%s_phy", lport_name[logical_id]);
 
247
        port->debugfs_lphy = debugfs_create_u32(debug_fs_name, 0644,
 
248
                        abe->debugfs_root, &port->physical_id);
 
249
        sprintf(debug_fs_name, "%s_users", lport_name[logical_id]);
 
250
        port->debugfs_lusers = debugfs_create_u32(debug_fs_name, 0644,
 
251
                        abe->debugfs_root, &port->users);
 
252
#endif
 
253
 
 
254
        pr_debug("opened port %s\n", lport_name[logical_id]);
 
255
        return port;
 
256
}
 
257
EXPORT_SYMBOL(omap_abe_port_open);
 
258
 
 
259
/*
 
260
 * omap_abe_port_close - close ABE logical port
 
261
 *
 
262
 * @port - logical ABE port to be closed (and disabled).
 
263
 */
 
264
void omap_abe_port_close(struct abe *abe, struct omap_abe_port *port)
 
265
{
 
266
        unsigned long flags;
 
267
 
 
268
        /* disable the port */
 
269
        omap_abe_port_disable(abe, port);
 
270
 
 
271
        spin_lock_irqsave(&abe->lock, flags);
 
272
        list_del(&port->list);
 
273
        spin_unlock_irqrestore(&abe->lock, flags);
 
274
 
 
275
        pr_debug("closed port %s\n", lport_name[port->logical_id]);
 
276
        kfree(port);
 
277
}
 
278
EXPORT_SYMBOL(omap_abe_port_close);
 
279
 
 
280
static struct abe *omap_abe_port_mgr_init(void)
 
281
{
 
282
        struct abe *abe;
 
283
 
 
284
        abe = kzalloc(sizeof(struct abe), GFP_KERNEL);
 
285
        if (abe == NULL)
 
286
                return NULL;
 
287
 
 
288
        spin_lock_init(&abe->lock);
 
289
 
 
290
        INIT_LIST_HEAD(&abe->ports);
 
291
        the_abe = abe;
 
292
 
 
293
#ifdef CONFIG_DEBUG_FS
 
294
        abe->debugfs_root = debugfs_create_dir("abe_port", NULL);
 
295
        if (!abe->debugfs_root) {
 
296
                pr_debug( "Failed to create port manager debugfs directory\n");
 
297
        }
 
298
#endif
 
299
        return abe;
 
300
}
 
301
 
 
302
static void omap_abe_port_mgr_free(struct abe *abe)
 
303
{
 
304
        debugfs_remove_recursive(abe->debugfs_root);
 
305
        kfree(abe);
 
306
        the_abe = NULL;
 
307
}
 
308
 
 
309
struct abe *omap_abe_port_mgr_get(void)
 
310
{
 
311
        struct abe * abe;
 
312
 
 
313
        mutex_lock(&port_mgr_mutex);
 
314
 
 
315
        if (the_abe)
 
316
                abe = the_abe;
 
317
        else
 
318
                abe = omap_abe_port_mgr_init();
 
319
 
 
320
        users++;
 
321
        mutex_unlock(&port_mgr_mutex);
 
322
        return abe;
 
323
}
 
324
EXPORT_SYMBOL(omap_abe_port_mgr_get);
 
325
 
 
326
void omap_abe_port_mgr_put(struct abe *abe)
 
327
{
 
328
        mutex_lock(&port_mgr_mutex);
 
329
 
 
330
        if (users == 0)
 
331
                goto out;
 
332
 
 
333
        if (--users == 0)
 
334
                omap_abe_port_mgr_free(abe);
 
335
 
 
336
out:
 
337
        mutex_unlock(&port_mgr_mutex);
 
338
}
 
339
EXPORT_SYMBOL(omap_abe_port_mgr_put);
 
340
 
 
341
MODULE_LICENSE("GPL");