blob: 8d6a9a969dbc246e87d46eb8bf38e0b62b796149 [file] [log] [blame]
Mark Brown2aa13b92010-10-20 00:00:11 +02001/*
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>
Mark Brown87d19062011-02-01 11:46:12 +000017#include <linux/pm.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020018#include <linux/spi/spi.h>
Mark Brown1df59812011-06-10 19:28:10 +010019#include <linux/regmap.h>
20#include <linux/err.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020021
22#include <linux/mfd/wm831x/core.h>
23
Mark Brown2aa13b92010-10-20 00:00:11 +020024static int __devinit wm831x_spi_probe(struct spi_device *spi)
25{
Mark Brown5570c2f2011-07-24 11:44:19 +010026 const struct spi_device_id *id = spi_get_device_id(spi);
Mark Brown2aa13b92010-10-20 00:00:11 +020027 struct wm831x *wm831x;
28 enum wm831x_parent type;
Mark Brown1df59812011-06-10 19:28:10 +010029 int ret;
Mark Brown2aa13b92010-10-20 00:00:11 +020030
Mark Brown5570c2f2011-07-24 11:44:19 +010031 type = (enum wm831x_parent)id->driver_data;
Mark Brown2aa13b92010-10-20 00:00:11 +020032
33 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
34 if (wm831x == NULL)
35 return -ENOMEM;
36
37 spi->bits_per_word = 16;
38 spi->mode = SPI_MODE_0;
39
40 dev_set_drvdata(&spi->dev, wm831x);
41 wm831x->dev = &spi->dev;
Mark Brown1df59812011-06-10 19:28:10 +010042
Mark Brown5570c2f2011-07-24 11:44:19 +010043 wm831x->regmap = regmap_init_spi(spi, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010044 if (IS_ERR(wm831x->regmap)) {
45 ret = PTR_ERR(wm831x->regmap);
46 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
47 ret);
48 kfree(wm831x);
49 return ret;
50 }
Mark Brown2aa13b92010-10-20 00:00:11 +020051
52 return wm831x_device_init(wm831x, type, spi->irq);
53}
54
55static int __devexit wm831x_spi_remove(struct spi_device *spi)
56{
57 struct wm831x *wm831x = dev_get_drvdata(&spi->dev);
58
59 wm831x_device_exit(wm831x);
60
61 return 0;
62}
63
Mark Brown87d19062011-02-01 11:46:12 +000064static int wm831x_spi_suspend(struct device *dev)
Mark Brown2aa13b92010-10-20 00:00:11 +020065{
Mark Brown87d19062011-02-01 11:46:12 +000066 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown2aa13b92010-10-20 00:00:11 +020067
68 return wm831x_device_suspend(wm831x);
69}
70
Mark Brown523d9cf2011-09-15 18:54:53 +020071static void wm831x_spi_shutdown(struct spi_device *spi)
72{
73 struct wm831x *wm831x = dev_get_drvdata(&spi->dev);
74
75 wm831x_device_shutdown(wm831x);
76}
77
Mark Brown87d19062011-02-01 11:46:12 +000078static const struct dev_pm_ops wm831x_spi_pm = {
79 .freeze = wm831x_spi_suspend,
80 .suspend = wm831x_spi_suspend,
81};
82
Mark Brown5570c2f2011-07-24 11:44:19 +010083static const struct spi_device_id wm831x_spi_ids[] = {
84 { "wm8310", WM8310 },
85 { "wm8311", WM8311 },
86 { "wm8312", WM8312 },
87 { "wm8320", WM8320 },
88 { "wm8321", WM8321 },
89 { "wm8325", WM8325 },
90 { "wm8326", WM8326 },
91 { },
Mark Brown2aa13b92010-10-20 00:00:11 +020092};
Mark Brown5570c2f2011-07-24 11:44:19 +010093MODULE_DEVICE_TABLE(spi, wm831x_spi_id);
Mark Brown2aa13b92010-10-20 00:00:11 +020094
Mark Brown5570c2f2011-07-24 11:44:19 +010095static struct spi_driver wm831x_spi_driver = {
Mark Brown2aa13b92010-10-20 00:00:11 +020096 .driver = {
Mark Brown5570c2f2011-07-24 11:44:19 +010097 .name = "wm831x",
Mark Brown2aa13b92010-10-20 00:00:11 +020098 .bus = &spi_bus_type,
99 .owner = THIS_MODULE,
Mark Brown87d19062011-02-01 11:46:12 +0000100 .pm = &wm831x_spi_pm,
Mark Brown2aa13b92010-10-20 00:00:11 +0200101 },
Mark Brown5570c2f2011-07-24 11:44:19 +0100102 .id_table = wm831x_spi_ids,
Mark Brown412dc112010-11-24 18:01:41 +0000103 .probe = wm831x_spi_probe,
104 .remove = __devexit_p(wm831x_spi_remove),
Mark Brown523d9cf2011-09-15 18:54:53 +0200105 .shutdown = wm831x_spi_shutdown,
Mark Brown412dc112010-11-24 18:01:41 +0000106};
107
Mark Brown2aa13b92010-10-20 00:00:11 +0200108static int __init wm831x_spi_init(void)
109{
110 int ret;
111
Mark Brown5570c2f2011-07-24 11:44:19 +0100112 ret = spi_register_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200113 if (ret != 0)
Mark Brown5570c2f2011-07-24 11:44:19 +0100114 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
Mark Brown412dc112010-11-24 18:01:41 +0000115
Mark Brown2aa13b92010-10-20 00:00:11 +0200116 return 0;
117}
118subsys_initcall(wm831x_spi_init);
119
120static void __exit wm831x_spi_exit(void)
121{
Mark Brown5570c2f2011-07-24 11:44:19 +0100122 spi_unregister_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200123}
124module_exit(wm831x_spi_exit);
125
126MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
127MODULE_LICENSE("GPL");
128MODULE_AUTHOR("Mark Brown");