blob: 7bcddccbf155795e115ac22e8f693c483b8e7112 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Brown2aa13b92010-10-20 00:00:11 +02002/*
3 * wm831x-spi.c -- SPI access for Wolfson WM831x PMICs
4 *
5 * Copyright 2009,2010 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown2aa13b92010-10-20 00:00:11 +02008 */
9
10#include <linux/kernel.h>
Paul Gortmakere85c5f02019-01-13 13:36:44 -050011#include <linux/init.h>
Charles Keepaxf6dd8442017-03-17 10:05:18 +000012#include <linux/of.h>
13#include <linux/of_device.h>
Mark Brown87d19062011-02-01 11:46:12 +000014#include <linux/pm.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020015#include <linux/spi/spi.h>
Mark Brown1df59812011-06-10 19:28:10 +010016#include <linux/regmap.h>
17#include <linux/err.h>
Mark Brown2aa13b92010-10-20 00:00:11 +020018
19#include <linux/mfd/wm831x/core.h>
20
Bill Pembertonf791be42012-11-19 13:23:04 -050021static int wm831x_spi_probe(struct spi_device *spi)
Mark Brown2aa13b92010-10-20 00:00:11 +020022{
Charles Keepaxf6dd8442017-03-17 10:05:18 +000023 struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
Mark Brown5570c2f2011-07-24 11:44:19 +010024 const struct spi_device_id *id = spi_get_device_id(spi);
Charles Keepaxf6dd8442017-03-17 10:05:18 +000025 const struct of_device_id *of_id;
Mark Brown2aa13b92010-10-20 00:00:11 +020026 struct wm831x *wm831x;
27 enum wm831x_parent type;
Mark Brown1df59812011-06-10 19:28:10 +010028 int ret;
Mark Brown2aa13b92010-10-20 00:00:11 +020029
Charles Keepaxf6dd8442017-03-17 10:05:18 +000030 if (spi->dev.of_node) {
31 of_id = of_match_device(wm831x_of_match, &spi->dev);
Gustavo A. R. Silva7b550332017-05-24 04:18:52 -050032 if (!of_id) {
33 dev_err(&spi->dev, "Failed to match device\n");
34 return -ENODEV;
35 }
Charles Keepaxf6dd8442017-03-17 10:05:18 +000036 type = (enum wm831x_parent)of_id->data;
37 } else {
38 type = (enum wm831x_parent)id->driver_data;
39 }
Mark Brown2aa13b92010-10-20 00:00:11 +020040
Mark Brownd48cbb72011-10-25 14:47:40 +020041 wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
Mark Brown2aa13b92010-10-20 00:00:11 +020042 if (wm831x == NULL)
43 return -ENOMEM;
44
Mark Brown2aa13b92010-10-20 00:00:11 +020045 spi->mode = SPI_MODE_0;
46
Jingoo Han0c0d2ab2013-04-06 15:42:48 +090047 spi_set_drvdata(spi, wm831x);
Mark Brown2aa13b92010-10-20 00:00:11 +020048 wm831x->dev = &spi->dev;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000049 wm831x->type = type;
Mark Brown1df59812011-06-10 19:28:10 +010050
Mark Brown130a7032012-01-30 20:08:06 +000051 wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010052 if (IS_ERR(wm831x->regmap)) {
53 ret = PTR_ERR(wm831x->regmap);
54 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
55 ret);
Mark Brown1df59812011-06-10 19:28:10 +010056 return ret;
57 }
Mark Brown2aa13b92010-10-20 00:00:11 +020058
Charles Keepaxf6dd8442017-03-17 10:05:18 +000059 if (pdata)
60 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
61
62 return wm831x_device_init(wm831x, spi->irq);
Mark Brown2aa13b92010-10-20 00:00:11 +020063}
64
Mark Brown87d19062011-02-01 11:46:12 +000065static int wm831x_spi_suspend(struct device *dev)
Mark Brown2aa13b92010-10-20 00:00:11 +020066{
Mark Brown87d19062011-02-01 11:46:12 +000067 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown2aa13b92010-10-20 00:00:11 +020068
69 return wm831x_device_suspend(wm831x);
70}
71
Mark Brownb9736a12013-11-08 18:51:25 +000072static int wm831x_spi_poweroff(struct device *dev)
Mark Brown523d9cf2011-09-15 18:54:53 +020073{
Mark Brownb9736a12013-11-08 18:51:25 +000074 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown523d9cf2011-09-15 18:54:53 +020075
76 wm831x_device_shutdown(wm831x);
Mark Brownb9736a12013-11-08 18:51:25 +000077
78 return 0;
Mark Brown523d9cf2011-09-15 18:54:53 +020079}
80
Mark Brown87d19062011-02-01 11:46:12 +000081static const struct dev_pm_ops wm831x_spi_pm = {
82 .freeze = wm831x_spi_suspend,
83 .suspend = wm831x_spi_suspend,
Mark Brownb9736a12013-11-08 18:51:25 +000084 .poweroff = wm831x_spi_poweroff,
Mark Brown87d19062011-02-01 11:46:12 +000085};
86
Mark Brown5570c2f2011-07-24 11:44:19 +010087static const struct spi_device_id wm831x_spi_ids[] = {
88 { "wm8310", WM8310 },
89 { "wm8311", WM8311 },
90 { "wm8312", WM8312 },
91 { "wm8320", WM8320 },
92 { "wm8321", WM8321 },
93 { "wm8325", WM8325 },
94 { "wm8326", WM8326 },
95 { },
Mark Brown2aa13b92010-10-20 00:00:11 +020096};
97
Mark Brown5570c2f2011-07-24 11:44:19 +010098static struct spi_driver wm831x_spi_driver = {
Mark Brown2aa13b92010-10-20 00:00:11 +020099 .driver = {
Mark Brown5570c2f2011-07-24 11:44:19 +0100100 .name = "wm831x",
Mark Brown87d19062011-02-01 11:46:12 +0000101 .pm = &wm831x_spi_pm,
Charles Keepaxf6dd8442017-03-17 10:05:18 +0000102 .of_match_table = of_match_ptr(wm831x_of_match),
Paul Gortmakere85c5f02019-01-13 13:36:44 -0500103 .suppress_bind_attrs = true,
Mark Brown2aa13b92010-10-20 00:00:11 +0200104 },
Mark Brown5570c2f2011-07-24 11:44:19 +0100105 .id_table = wm831x_spi_ids,
Mark Brown412dc112010-11-24 18:01:41 +0000106 .probe = wm831x_spi_probe,
Mark Brown412dc112010-11-24 18:01:41 +0000107};
108
Mark Brown2aa13b92010-10-20 00:00:11 +0200109static int __init wm831x_spi_init(void)
110{
111 int ret;
112
Mark Brown5570c2f2011-07-24 11:44:19 +0100113 ret = spi_register_driver(&wm831x_spi_driver);
Mark Brown2aa13b92010-10-20 00:00:11 +0200114 if (ret != 0)
Mark Brown5570c2f2011-07-24 11:44:19 +0100115 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
Mark Brown412dc112010-11-24 18:01:41 +0000116
Mark Brown2aa13b92010-10-20 00:00:11 +0200117 return 0;
118}
119subsys_initcall(wm831x_spi_init);