blob: c332e2885b26e2a5a1cbe50801149b6d48eca3ca [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>
Charles Keepaxf6dd8442017-03-17 10:05:18 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Mark Brown87d19062011-02-01 11:46:12 +000019#include <linux/pm.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020020#include <linux/spi/spi.h>
Mark Brown1df59812011-06-10 19:28:10 +010021#include <linux/regmap.h>
22#include <linux/err.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020023
24#include <linux/mfd/wm831x/core.h>
25
Bill Pembertonf791be42012-11-19 13:23:04 -050026static int wm831x_spi_probe(struct spi_device *spi)
Mark Brown2aa13b92010-10-20 00:00:11 +020027{
Charles Keepaxf6dd8442017-03-17 10:05:18 +000028 struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
Mark Brown5570c2f2011-07-24 11:44:19 +010029 const struct spi_device_id *id = spi_get_device_id(spi);
Charles Keepaxf6dd8442017-03-17 10:05:18 +000030 const struct of_device_id *of_id;
Mark Brown2aa13b92010-10-20 00:00:11 +020031 struct wm831x *wm831x;
32 enum wm831x_parent type;
Mark Brown1df59812011-06-10 19:28:10 +010033 int ret;
Mark Brown2aa13b92010-10-20 00:00:11 +020034
Charles Keepaxf6dd8442017-03-17 10:05:18 +000035 if (spi->dev.of_node) {
36 of_id = of_match_device(wm831x_of_match, &spi->dev);
37 type = (enum wm831x_parent)of_id->data;
38 } else {
39 type = (enum wm831x_parent)id->driver_data;
40 }
Mark Brown2aa13b92010-10-20 00:00:11 +020041
Mark Brownd48cbb72011-10-25 14:47:40 +020042 wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
Mark Brown2aa13b92010-10-20 00:00:11 +020043 if (wm831x == NULL)
44 return -ENOMEM;
45
Mark Brown2aa13b92010-10-20 00:00:11 +020046 spi->mode = SPI_MODE_0;
47
Jingoo Han0c0d2ab2013-04-06 15:42:48 +090048 spi_set_drvdata(spi, wm831x);
Mark Brown2aa13b92010-10-20 00:00:11 +020049 wm831x->dev = &spi->dev;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000050 wm831x->type = type;
Mark Brown1df59812011-06-10 19:28:10 +010051
Mark Brown130a7032012-01-30 20:08:06 +000052 wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010053 if (IS_ERR(wm831x->regmap)) {
54 ret = PTR_ERR(wm831x->regmap);
55 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
56 ret);
Mark Brown1df59812011-06-10 19:28:10 +010057 return ret;
58 }
Mark Brown2aa13b92010-10-20 00:00:11 +020059
Charles Keepaxf6dd8442017-03-17 10:05:18 +000060 if (pdata)
61 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
62
63 return wm831x_device_init(wm831x, spi->irq);
Mark Brown2aa13b92010-10-20 00:00:11 +020064}
65
Bill Pemberton4740f732012-11-19 13:26:01 -050066static int wm831x_spi_remove(struct spi_device *spi)
Mark Brown2aa13b92010-10-20 00:00:11 +020067{
Jingoo Han0c0d2ab2013-04-06 15:42:48 +090068 struct wm831x *wm831x = spi_get_drvdata(spi);
Mark Brown2aa13b92010-10-20 00:00:11 +020069
70 wm831x_device_exit(wm831x);
71
72 return 0;
73}
74
Mark Brown87d19062011-02-01 11:46:12 +000075static int wm831x_spi_suspend(struct device *dev)
Mark Brown2aa13b92010-10-20 00:00:11 +020076{
Mark Brown87d19062011-02-01 11:46:12 +000077 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown2aa13b92010-10-20 00:00:11 +020078
79 return wm831x_device_suspend(wm831x);
80}
81
Mark Brownb9736a12013-11-08 18:51:25 +000082static int wm831x_spi_poweroff(struct device *dev)
Mark Brown523d9cf2011-09-15 18:54:53 +020083{
Mark Brownb9736a12013-11-08 18:51:25 +000084 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown523d9cf2011-09-15 18:54:53 +020085
86 wm831x_device_shutdown(wm831x);
Mark Brownb9736a12013-11-08 18:51:25 +000087
88 return 0;
Mark Brown523d9cf2011-09-15 18:54:53 +020089}
90
Mark Brown87d19062011-02-01 11:46:12 +000091static const struct dev_pm_ops wm831x_spi_pm = {
92 .freeze = wm831x_spi_suspend,
93 .suspend = wm831x_spi_suspend,
Mark Brownb9736a12013-11-08 18:51:25 +000094 .poweroff = wm831x_spi_poweroff,
Mark Brown87d19062011-02-01 11:46:12 +000095};
96
Mark Brown5570c2f2011-07-24 11:44:19 +010097static const struct spi_device_id wm831x_spi_ids[] = {
98 { "wm8310", WM8310 },
99 { "wm8311", WM8311 },
100 { "wm8312", WM8312 },
101 { "wm8320", WM8320 },
102 { "wm8321", WM8321 },
103 { "wm8325", WM8325 },
104 { "wm8326", WM8326 },
105 { },
Mark Brown2aa13b92010-10-20 00:00:11 +0200106};
Hai Dongfe2afaa2012-02-24 16:03:00 -0600107MODULE_DEVICE_TABLE(spi, wm831x_spi_ids);
Mark Brown2aa13b92010-10-20 00:00:11 +0200108
Mark Brown5570c2f2011-07-24 11:44:19 +0100109static struct spi_driver wm831x_spi_driver = {
Mark Brown2aa13b92010-10-20 00:00:11 +0200110 .driver = {
Mark Brown5570c2f2011-07-24 11:44:19 +0100111 .name = "wm831x",
Mark Brown87d19062011-02-01 11:46:12 +0000112 .pm = &wm831x_spi_pm,
Charles Keepaxf6dd8442017-03-17 10:05:18 +0000113 .of_match_table = of_match_ptr(wm831x_of_match),
Mark Brown2aa13b92010-10-20 00:00:11 +0200114 },
Mark Brown5570c2f2011-07-24 11:44:19 +0100115 .id_table = wm831x_spi_ids,
Mark Brown412dc112010-11-24 18:01:41 +0000116 .probe = wm831x_spi_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500117 .remove = wm831x_spi_remove,
Mark Brown412dc112010-11-24 18:01:41 +0000118};
119
Mark Brown2aa13b92010-10-20 00:00:11 +0200120static int __init wm831x_spi_init(void)
121{
122 int ret;
123
Mark Brown5570c2f2011-07-24 11:44:19 +0100124 ret = spi_register_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200125 if (ret != 0)
Mark Brown5570c2f2011-07-24 11:44:19 +0100126 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
Mark Brown412dc112010-11-24 18:01:41 +0000127
Mark Brown2aa13b92010-10-20 00:00:11 +0200128 return 0;
129}
130subsys_initcall(wm831x_spi_init);
131
132static void __exit wm831x_spi_exit(void)
133{
Mark Brown5570c2f2011-07-24 11:44:19 +0100134 spi_unregister_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200135}
136module_exit(wm831x_spi_exit);
137
138MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
139MODULE_LICENSE("GPL");
140MODULE_AUTHOR("Mark Brown");