mfd: Convert WM831x to use regmap API
[linux-2.6.git] / drivers / mfd / wm831x-spi.c
1 /*
2  * wm831x-spi.c  --  SPI access for Wolfson WM831x PMICs
3  *
4  * Copyright 2009,2010 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pm.h>
18 #include <linux/spi/spi.h>
19 #include <linux/regmap.h>
20 #include <linux/err.h>
21
22 #include <linux/mfd/wm831x/core.h>
23
24 static int __devinit wm831x_spi_probe(struct spi_device *spi)
25 {
26         struct wm831x *wm831x;
27         enum wm831x_parent type;
28         int ret;
29
30         /* Currently SPI support for ID tables is unmerged, we're faking it */
31         if (strcmp(spi->modalias, "wm8310") == 0)
32                 type = WM8310;
33         else if (strcmp(spi->modalias, "wm8311") == 0)
34                 type = WM8311;
35         else if (strcmp(spi->modalias, "wm8312") == 0)
36                 type = WM8312;
37         else if (strcmp(spi->modalias, "wm8320") == 0)
38                 type = WM8320;
39         else if (strcmp(spi->modalias, "wm8321") == 0)
40                 type = WM8321;
41         else if (strcmp(spi->modalias, "wm8325") == 0)
42                 type = WM8325;
43         else if (strcmp(spi->modalias, "wm8326") == 0)
44                 type = WM8326;
45         else {
46                 dev_err(&spi->dev, "Unknown device type\n");
47                 return -EINVAL;
48         }
49
50         wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
51         if (wm831x == NULL)
52                 return -ENOMEM;
53
54         spi->bits_per_word = 16;
55         spi->mode = SPI_MODE_0;
56
57         dev_set_drvdata(&spi->dev, wm831x);
58         wm831x->dev = &spi->dev;
59
60         wm831x->regmap = regmap_init_spi(wm831x->dev, &wm831x_regmap_config);
61         if (IS_ERR(wm831x->regmap)) {
62                 ret = PTR_ERR(wm831x->regmap);
63                 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
64                         ret);
65                 kfree(wm831x);
66                 return ret;
67         }
68
69         return wm831x_device_init(wm831x, type, spi->irq);
70 }
71
72 static int __devexit wm831x_spi_remove(struct spi_device *spi)
73 {
74         struct wm831x *wm831x = dev_get_drvdata(&spi->dev);
75
76         wm831x_device_exit(wm831x);
77
78         return 0;
79 }
80
81 static int wm831x_spi_suspend(struct device *dev)
82 {
83         struct wm831x *wm831x = dev_get_drvdata(dev);
84
85         return wm831x_device_suspend(wm831x);
86 }
87
88 static const struct dev_pm_ops wm831x_spi_pm = {
89         .freeze = wm831x_spi_suspend,
90         .suspend = wm831x_spi_suspend,
91 };
92
93 static struct spi_driver wm8310_spi_driver = {
94         .driver = {
95                 .name   = "wm8310",
96                 .bus    = &spi_bus_type,
97                 .owner  = THIS_MODULE,
98                 .pm     = &wm831x_spi_pm,
99         },
100         .probe          = wm831x_spi_probe,
101         .remove         = __devexit_p(wm831x_spi_remove),
102 };
103
104 static struct spi_driver wm8311_spi_driver = {
105         .driver = {
106                 .name   = "wm8311",
107                 .bus    = &spi_bus_type,
108                 .owner  = THIS_MODULE,
109                 .pm     = &wm831x_spi_pm,
110         },
111         .probe          = wm831x_spi_probe,
112         .remove         = __devexit_p(wm831x_spi_remove),
113 };
114
115 static struct spi_driver wm8312_spi_driver = {
116         .driver = {
117                 .name   = "wm8312",
118                 .bus    = &spi_bus_type,
119                 .owner  = THIS_MODULE,
120                 .pm     = &wm831x_spi_pm,
121         },
122         .probe          = wm831x_spi_probe,
123         .remove         = __devexit_p(wm831x_spi_remove),
124 };
125
126 static struct spi_driver wm8320_spi_driver = {
127         .driver = {
128                 .name   = "wm8320",
129                 .bus    = &spi_bus_type,
130                 .owner  = THIS_MODULE,
131                 .pm     = &wm831x_spi_pm,
132         },
133         .probe          = wm831x_spi_probe,
134         .remove         = __devexit_p(wm831x_spi_remove),
135 };
136
137 static struct spi_driver wm8321_spi_driver = {
138         .driver = {
139                 .name   = "wm8321",
140                 .bus    = &spi_bus_type,
141                 .owner  = THIS_MODULE,
142                 .pm     = &wm831x_spi_pm,
143         },
144         .probe          = wm831x_spi_probe,
145         .remove         = __devexit_p(wm831x_spi_remove),
146 };
147
148 static struct spi_driver wm8325_spi_driver = {
149         .driver = {
150                 .name   = "wm8325",
151                 .bus    = &spi_bus_type,
152                 .owner  = THIS_MODULE,
153                 .pm     = &wm831x_spi_pm,
154         },
155         .probe          = wm831x_spi_probe,
156         .remove         = __devexit_p(wm831x_spi_remove),
157 };
158
159 static struct spi_driver wm8326_spi_driver = {
160         .driver = {
161                 .name   = "wm8326",
162                 .bus    = &spi_bus_type,
163                 .owner  = THIS_MODULE,
164                 .pm     = &wm831x_spi_pm,
165         },
166         .probe          = wm831x_spi_probe,
167         .remove         = __devexit_p(wm831x_spi_remove),
168 };
169
170 static int __init wm831x_spi_init(void)
171 {
172         int ret;
173
174         ret = spi_register_driver(&wm8310_spi_driver);
175         if (ret != 0)
176                 pr_err("Failed to register WM8310 SPI driver: %d\n", ret);
177
178         ret = spi_register_driver(&wm8311_spi_driver);
179         if (ret != 0)
180                 pr_err("Failed to register WM8311 SPI driver: %d\n", ret);
181
182         ret = spi_register_driver(&wm8312_spi_driver);
183         if (ret != 0)
184                 pr_err("Failed to register WM8312 SPI driver: %d\n", ret);
185
186         ret = spi_register_driver(&wm8320_spi_driver);
187         if (ret != 0)
188                 pr_err("Failed to register WM8320 SPI driver: %d\n", ret);
189
190         ret = spi_register_driver(&wm8321_spi_driver);
191         if (ret != 0)
192                 pr_err("Failed to register WM8321 SPI driver: %d\n", ret);
193
194         ret = spi_register_driver(&wm8325_spi_driver);
195         if (ret != 0)
196                 pr_err("Failed to register WM8325 SPI driver: %d\n", ret);
197
198         ret = spi_register_driver(&wm8326_spi_driver);
199         if (ret != 0)
200                 pr_err("Failed to register WM8326 SPI driver: %d\n", ret);
201
202         return 0;
203 }
204 subsys_initcall(wm831x_spi_init);
205
206 static void __exit wm831x_spi_exit(void)
207 {
208         spi_unregister_driver(&wm8326_spi_driver);
209         spi_unregister_driver(&wm8325_spi_driver);
210         spi_unregister_driver(&wm8321_spi_driver);
211         spi_unregister_driver(&wm8320_spi_driver);
212         spi_unregister_driver(&wm8312_spi_driver);
213         spi_unregister_driver(&wm8311_spi_driver);
214         spi_unregister_driver(&wm8310_spi_driver);
215 }
216 module_exit(wm831x_spi_exit);
217
218 MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
219 MODULE_LICENSE("GPL");
220 MODULE_AUTHOR("Mark Brown");