Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2aa13b9e MB |
2 | /* |
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> | |
2aa13b9e MB |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
e85c5f0a | 11 | #include <linux/init.h> |
f6dd8449 CK |
12 | #include <linux/of.h> |
13 | #include <linux/of_device.h> | |
87d1906d | 14 | #include <linux/pm.h> |
2aa13b9e | 15 | #include <linux/spi/spi.h> |
1df5981b MB |
16 | #include <linux/regmap.h> |
17 | #include <linux/err.h> | |
2aa13b9e MB |
18 | |
19 | #include <linux/mfd/wm831x/core.h> | |
20 | ||
f791be49 | 21 | static int wm831x_spi_probe(struct spi_device *spi) |
2aa13b9e | 22 | { |
f6dd8449 | 23 | struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev); |
5570c2f7 | 24 | const struct spi_device_id *id = spi_get_device_id(spi); |
f6dd8449 | 25 | const struct of_device_id *of_id; |
2aa13b9e MB |
26 | struct wm831x *wm831x; |
27 | enum wm831x_parent type; | |
1df5981b | 28 | int ret; |
2aa13b9e | 29 | |
f6dd8449 CK |
30 | if (spi->dev.of_node) { |
31 | of_id = of_match_device(wm831x_of_match, &spi->dev); | |
7b55033f GS |
32 | if (!of_id) { |
33 | dev_err(&spi->dev, "Failed to match device\n"); | |
34 | return -ENODEV; | |
35 | } | |
f6dd8449 CK |
36 | type = (enum wm831x_parent)of_id->data; |
37 | } else { | |
38 | type = (enum wm831x_parent)id->driver_data; | |
39 | } | |
2aa13b9e | 40 | |
d48cbb74 | 41 | wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL); |
2aa13b9e MB |
42 | if (wm831x == NULL) |
43 | return -ENOMEM; | |
44 | ||
2aa13b9e MB |
45 | spi->mode = SPI_MODE_0; |
46 | ||
0c0d2ab0 | 47 | spi_set_drvdata(spi, wm831x); |
2aa13b9e | 48 | wm831x->dev = &spi->dev; |
f6dd8449 | 49 | wm831x->type = type; |
1df5981b | 50 | |
130a7032 | 51 | wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config); |
1df5981b MB |
52 | 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); | |
1df5981b MB |
56 | return ret; |
57 | } | |
2aa13b9e | 58 | |
f6dd8449 CK |
59 | if (pdata) |
60 | memcpy(&wm831x->pdata, pdata, sizeof(*pdata)); | |
61 | ||
62 | return wm831x_device_init(wm831x, spi->irq); | |
2aa13b9e MB |
63 | } |
64 | ||
87d1906d | 65 | static int wm831x_spi_suspend(struct device *dev) |
2aa13b9e | 66 | { |
87d1906d | 67 | struct wm831x *wm831x = dev_get_drvdata(dev); |
2aa13b9e MB |
68 | |
69 | return wm831x_device_suspend(wm831x); | |
70 | } | |
71 | ||
b9736a16 | 72 | static int wm831x_spi_poweroff(struct device *dev) |
523d9cfb | 73 | { |
b9736a16 | 74 | struct wm831x *wm831x = dev_get_drvdata(dev); |
523d9cfb MB |
75 | |
76 | wm831x_device_shutdown(wm831x); | |
b9736a16 MB |
77 | |
78 | return 0; | |
523d9cfb MB |
79 | } |
80 | ||
87d1906d MB |
81 | static const struct dev_pm_ops wm831x_spi_pm = { |
82 | .freeze = wm831x_spi_suspend, | |
83 | .suspend = wm831x_spi_suspend, | |
b9736a16 | 84 | .poweroff = wm831x_spi_poweroff, |
87d1906d MB |
85 | }; |
86 | ||
5570c2f7 MB |
87 | static 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 | { }, | |
2aa13b9e MB |
96 | }; |
97 | ||
5570c2f7 | 98 | static struct spi_driver wm831x_spi_driver = { |
2aa13b9e | 99 | .driver = { |
5570c2f7 | 100 | .name = "wm831x", |
87d1906d | 101 | .pm = &wm831x_spi_pm, |
f6dd8449 | 102 | .of_match_table = of_match_ptr(wm831x_of_match), |
e85c5f0a | 103 | .suppress_bind_attrs = true, |
412dc11d | 104 | }, |
5570c2f7 | 105 | .id_table = wm831x_spi_ids, |
412dc11d | 106 | .probe = wm831x_spi_probe, |
412dc11d MB |
107 | }; |
108 | ||
2aa13b9e MB |
109 | static int __init wm831x_spi_init(void) |
110 | { | |
111 | int ret; | |
112 | ||
5570c2f7 | 113 | ret = spi_register_driver(&wm831x_spi_driver); |
412dc11d | 114 | if (ret != 0) |
5570c2f7 | 115 | pr_err("Failed to register WM831x SPI driver: %d\n", ret); |
412dc11d | 116 | |
2aa13b9e MB |
117 | return 0; |
118 | } | |
119 | subsys_initcall(wm831x_spi_init); |