blob: 80482aeb246a4bd77e5828f5720b481005d365f3 [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
Bill Pembertonf791be42012-11-19 13:23:04 -050024static int wm831x_spi_probe(struct spi_device *spi)
Mark Brown2aa13b92010-10-20 00:00:11 +020025{
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
Mark Brownd48cbb72011-10-25 14:47:40 +020033 wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
Mark Brown2aa13b92010-10-20 00:00:11 +020034 if (wm831x == NULL)
35 return -ENOMEM;
36
Mark Brown2aa13b92010-10-20 00:00:11 +020037 spi->mode = SPI_MODE_0;
38
Jingoo Han0c0d2ab2013-04-06 15:42:48 +090039 spi_set_drvdata(spi, wm831x);
Mark Brown2aa13b92010-10-20 00:00:11 +020040 wm831x->dev = &spi->dev;
Mark Brown1df59812011-06-10 19:28:10 +010041
Mark Brown130a7032012-01-30 20:08:06 +000042 wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010043 if (IS_ERR(wm831x->regmap)) {
44 ret = PTR_ERR(wm831x->regmap);
45 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
46 ret);
Mark Brown1df59812011-06-10 19:28:10 +010047 return ret;
48 }
Mark Brown2aa13b92010-10-20 00:00:11 +020049
50 return wm831x_device_init(wm831x, type, spi->irq);
51}
52
Bill Pemberton4740f732012-11-19 13:26:01 -050053static int wm831x_spi_remove(struct spi_device *spi)
Mark Brown2aa13b92010-10-20 00:00:11 +020054{
Jingoo Han0c0d2ab2013-04-06 15:42:48 +090055 struct wm831x *wm831x = spi_get_drvdata(spi);
Mark Brown2aa13b92010-10-20 00:00:11 +020056
57 wm831x_device_exit(wm831x);
58
59 return 0;
60}
61
Mark Brown87d19062011-02-01 11:46:12 +000062static int wm831x_spi_suspend(struct device *dev)
Mark Brown2aa13b92010-10-20 00:00:11 +020063{
Mark Brown87d19062011-02-01 11:46:12 +000064 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown2aa13b92010-10-20 00:00:11 +020065
66 return wm831x_device_suspend(wm831x);
67}
68
Mark Brownb9736a12013-11-08 18:51:25 +000069static int wm831x_spi_poweroff(struct device *dev)
Mark Brown523d9cf2011-09-15 18:54:53 +020070{
Mark Brownb9736a12013-11-08 18:51:25 +000071 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown523d9cf2011-09-15 18:54:53 +020072
73 wm831x_device_shutdown(wm831x);
Mark Brownb9736a12013-11-08 18:51:25 +000074
75 return 0;
Mark Brown523d9cf2011-09-15 18:54:53 +020076}
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,
Mark Brownb9736a12013-11-08 18:51:25 +000081 .poweroff = wm831x_spi_poweroff,
Mark Brown87d19062011-02-01 11:46:12 +000082};
83
Mark Brown5570c2f2011-07-24 11:44:19 +010084static const struct spi_device_id wm831x_spi_ids[] = {
85 { "wm8310", WM8310 },
86 { "wm8311", WM8311 },
87 { "wm8312", WM8312 },
88 { "wm8320", WM8320 },
89 { "wm8321", WM8321 },
90 { "wm8325", WM8325 },
91 { "wm8326", WM8326 },
92 { },
Mark Brown2aa13b92010-10-20 00:00:11 +020093};
Hai Dongfe2afaa2012-02-24 16:03:00 -060094MODULE_DEVICE_TABLE(spi, wm831x_spi_ids);
Mark Brown2aa13b92010-10-20 00:00:11 +020095
Mark Brown5570c2f2011-07-24 11:44:19 +010096static struct spi_driver wm831x_spi_driver = {
Mark Brown2aa13b92010-10-20 00:00:11 +020097 .driver = {
Mark Brown5570c2f2011-07-24 11:44:19 +010098 .name = "wm831x",
Mark Brown87d19062011-02-01 11:46:12 +000099 .pm = &wm831x_spi_pm,
Mark Brown2aa13b92010-10-20 00:00:11 +0200100 },
Mark Brown5570c2f2011-07-24 11:44:19 +0100101 .id_table = wm831x_spi_ids,
Mark Brown412dc112010-11-24 18:01:41 +0000102 .probe = wm831x_spi_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500103 .remove = wm831x_spi_remove,
Mark Brown412dc112010-11-24 18:01:41 +0000104};
105
Mark Brown2aa13b92010-10-20 00:00:11 +0200106static int __init wm831x_spi_init(void)
107{
108 int ret;
109
Mark Brown5570c2f2011-07-24 11:44:19 +0100110 ret = spi_register_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200111 if (ret != 0)
Mark Brown5570c2f2011-07-24 11:44:19 +0100112 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
Mark Brown412dc112010-11-24 18:01:41 +0000113
Mark Brown2aa13b92010-10-20 00:00:11 +0200114 return 0;
115}
116subsys_initcall(wm831x_spi_init);
117
118static void __exit wm831x_spi_exit(void)
119{
Mark Brown5570c2f2011-07-24 11:44:19 +0100120 spi_unregister_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200121}
122module_exit(wm831x_spi_exit);
123
124MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
125MODULE_LICENSE("GPL");
126MODULE_AUTHOR("Mark Brown");