Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6] / sound / aoa / soundbus / i2sbus / i2sbus-control.c
1 /*
2  * i2sbus driver -- bus control routines
3  *
4  * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5  *
6  * GPL v2, can be found in COPYING.
7  */
8
9 #include <asm/io.h>
10 #include <linux/delay.h>
11 #include <asm/prom.h>
12 #include <asm/macio.h>
13 #include <asm/pmac_feature.h>
14 #include <asm/pmac_pfunc.h>
15 #include "i2sbus.h"
16
17 int i2sbus_control_init(struct macio_dev* dev, struct i2sbus_control **c)
18 {
19         *c = kzalloc(sizeof(struct i2sbus_control), GFP_KERNEL);
20         if (!*c)
21                 return -ENOMEM;
22
23         INIT_LIST_HEAD(&(*c)->list);
24
25         if (of_address_to_resource(dev->ofdev.node, 0, &(*c)->rsrc))
26                 goto err;
27         /* we really should be using feature calls instead of mapping
28          * these registers. It's safe for now since no one else is
29          * touching them... */
30         (*c)->controlregs = ioremap((*c)->rsrc.start,
31                                     sizeof(struct i2s_control_regs));
32         if (!(*c)->controlregs)
33                 goto err;
34
35         return 0;
36  err:
37         kfree(*c);
38         *c = NULL;
39         return -ENODEV;
40 }
41
42 void i2sbus_control_destroy(struct i2sbus_control *c)
43 {
44         iounmap(c->controlregs);
45         kfree(c);
46 }
47
48 /* this is serialised externally */
49 int i2sbus_control_add_dev(struct i2sbus_control *c,
50                            struct i2sbus_dev *i2sdev)
51 {
52         struct device_node *np;
53
54         np = i2sdev->sound.ofdev.node;
55         i2sdev->enable = pmf_find_function(np, "enable");
56         i2sdev->cell_enable = pmf_find_function(np, "cell-enable");
57         i2sdev->clock_enable = pmf_find_function(np, "clock-enable");
58         i2sdev->cell_disable = pmf_find_function(np, "cell-disable");
59         i2sdev->clock_disable = pmf_find_function(np, "clock-disable");
60
61         /* if the bus number is not 0 or 1 we absolutely need to use
62          * the platform functions -- there's nothing in Darwin that
63          * would allow seeing a system behind what the FCRs are then,
64          * and I don't want to go parsing a bunch of platform functions
65          * by hand to try finding a system... */
66         if (i2sdev->bus_number != 0 && i2sdev->bus_number != 1 &&
67             (!i2sdev->enable ||
68              !i2sdev->cell_enable || !i2sdev->clock_enable ||
69              !i2sdev->cell_disable || !i2sdev->clock_disable)) {
70                 pmf_put_function(i2sdev->enable);
71                 pmf_put_function(i2sdev->cell_enable);
72                 pmf_put_function(i2sdev->clock_enable);
73                 pmf_put_function(i2sdev->cell_disable);
74                 pmf_put_function(i2sdev->clock_disable);
75                 return -ENODEV;
76         }
77
78         list_add(&i2sdev->item, &c->list);
79
80         return 0;
81 }
82
83 void i2sbus_control_remove_dev(struct i2sbus_control *c,
84                                struct i2sbus_dev *i2sdev)
85 {
86         /* this is serialised externally */
87         list_del(&i2sdev->item);
88         if (list_empty(&c->list))
89                 i2sbus_control_destroy(c);
90 }
91
92 int i2sbus_control_enable(struct i2sbus_control *c,
93                           struct i2sbus_dev *i2sdev)
94 {
95         struct pmf_args args = { .count = 0 };
96         int cc;
97
98         if (i2sdev->enable)
99                 return pmf_call_one(i2sdev->enable, &args);
100
101         switch (i2sdev->bus_number) {
102         case 0:
103                 cc = in_le32(&c->controlregs->cell_control);
104                 out_le32(&c->controlregs->cell_control, cc | CTRL_CLOCK_INTF_0_ENABLE);
105                 break;
106         case 1:
107                 cc = in_le32(&c->controlregs->cell_control);
108                 out_le32(&c->controlregs->cell_control, cc | CTRL_CLOCK_INTF_1_ENABLE);
109                 break;
110         default:
111                 return -ENODEV;
112         }
113         return 0;
114 }
115
116 int i2sbus_control_cell(struct i2sbus_control *c,
117                         struct i2sbus_dev *i2sdev,
118                         int enable)
119 {
120         struct pmf_args args = { .count = 0 };
121         int cc;
122
123         switch (enable) {
124         case 0:
125                 if (i2sdev->cell_disable)
126                         return pmf_call_one(i2sdev->cell_disable, &args);
127                 break;
128         case 1:
129                 if (i2sdev->cell_enable)
130                         return pmf_call_one(i2sdev->cell_enable, &args);
131                 break;
132         default:
133                 printk(KERN_ERR "i2sbus: INVALID CELL ENABLE VALUE\n");
134                 return -ENODEV;
135         }
136         switch (i2sdev->bus_number) {
137         case 0:
138                 cc = in_le32(&c->controlregs->cell_control);
139                 cc &= ~CTRL_CLOCK_CELL_0_ENABLE;
140                 cc |= enable * CTRL_CLOCK_CELL_0_ENABLE;
141                 out_le32(&c->controlregs->cell_control, cc);
142                 break;
143         case 1:
144                 cc = in_le32(&c->controlregs->cell_control);
145                 cc &= ~CTRL_CLOCK_CELL_1_ENABLE;
146                 cc |= enable * CTRL_CLOCK_CELL_1_ENABLE;
147                 out_le32(&c->controlregs->cell_control, cc);
148                 break;
149         default:
150                 return -ENODEV;
151         }
152         return 0;
153 }
154
155 int i2sbus_control_clock(struct i2sbus_control *c,
156                          struct i2sbus_dev *i2sdev,
157                          int enable)
158 {
159         struct pmf_args args = { .count = 0 };
160         int cc;
161
162         switch (enable) {
163         case 0:
164                 if (i2sdev->clock_disable)
165                         return pmf_call_one(i2sdev->clock_disable, &args);
166                 break;
167         case 1:
168                 if (i2sdev->clock_enable)
169                         return pmf_call_one(i2sdev->clock_enable, &args);
170                 break;
171         default:
172                 printk(KERN_ERR "i2sbus: INVALID CLOCK ENABLE VALUE\n");
173                 return -ENODEV;
174         }
175         switch (i2sdev->bus_number) {
176         case 0:
177                 cc = in_le32(&c->controlregs->cell_control);
178                 cc &= ~CTRL_CLOCK_CLOCK_0_ENABLE;
179                 cc |= enable * CTRL_CLOCK_CLOCK_0_ENABLE;
180                 out_le32(&c->controlregs->cell_control, cc);
181                 break;
182         case 1:
183                 cc = in_le32(&c->controlregs->cell_control);
184                 cc &= ~CTRL_CLOCK_CLOCK_1_ENABLE;
185                 cc |= enable * CTRL_CLOCK_CLOCK_1_ENABLE;
186                 out_le32(&c->controlregs->cell_control, cc);
187                 break;
188         default:
189                 return -ENODEV;
190         }
191         return 0;
192 }