Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
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>
8 */
9
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/of.h>
13#include <linux/of_device.h>
14#include <linux/pm.h>
15#include <linux/spi/spi.h>
16#include <linux/regmap.h>
17#include <linux/err.h>
18
19#include <linux/mfd/wm831x/core.h>
20
21static int wm831x_spi_probe(struct spi_device *spi)
22{
23 struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
24 const struct spi_device_id *id = spi_get_device_id(spi);
25 const struct of_device_id *of_id;
26 struct wm831x *wm831x;
27 enum wm831x_parent type;
28 int ret;
29
30 if (spi->dev.of_node) {
31 of_id = of_match_device(wm831x_of_match, &spi->dev);
32 if (!of_id) {
33 dev_err(&spi->dev, "Failed to match device\n");
34 return -ENODEV;
35 }
36 type = (enum wm831x_parent)of_id->data;
37 } else {
38 type = (enum wm831x_parent)id->driver_data;
39 }
40
41 wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
42 if (wm831x == NULL)
43 return -ENOMEM;
44
45 spi->mode = SPI_MODE_0;
46
47 spi_set_drvdata(spi, wm831x);
48 wm831x->dev = &spi->dev;
49 wm831x->type = type;
50
51 wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
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);
56 return ret;
57 }
58
59 if (pdata)
60 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
61
62 return wm831x_device_init(wm831x, spi->irq);
63}
64
65static int wm831x_spi_suspend(struct device *dev)
66{
67 struct wm831x *wm831x = dev_get_drvdata(dev);
68
69 return wm831x_device_suspend(wm831x);
70}
71
72static int wm831x_spi_poweroff(struct device *dev)
73{
74 struct wm831x *wm831x = dev_get_drvdata(dev);
75
76 wm831x_device_shutdown(wm831x);
77
78 return 0;
79}
80
81static const struct dev_pm_ops wm831x_spi_pm = {
82 .freeze = wm831x_spi_suspend,
83 .suspend = wm831x_spi_suspend,
84 .poweroff = wm831x_spi_poweroff,
85};
86
87static 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 { },
96};
97
98static struct spi_driver wm831x_spi_driver = {
99 .driver = {
100 .name = "wm831x",
101 .pm = &wm831x_spi_pm,
102 .of_match_table = of_match_ptr(wm831x_of_match),
103 .suppress_bind_attrs = true,
104 },
105 .id_table = wm831x_spi_ids,
106 .probe = wm831x_spi_probe,
107};
108
109static int __init wm831x_spi_init(void)
110{
111 int ret;
112
113 ret = spi_register_driver(&wm831x_spi_driver);
114 if (ret != 0)
115 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
116
117 return 0;
118}
119subsys_initcall(wm831x_spi_init);
1/*
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>
17#include <linux/pm.h>
18#include <linux/spi/spi.h>
19#include <linux/regmap.h>
20#include <linux/err.h>
21
22#include <linux/mfd/wm831x/core.h>
23
24static int wm831x_spi_probe(struct spi_device *spi)
25{
26 const struct spi_device_id *id = spi_get_device_id(spi);
27 struct wm831x *wm831x;
28 enum wm831x_parent type;
29 int ret;
30
31 type = (enum wm831x_parent)id->driver_data;
32
33 wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
34 if (wm831x == NULL)
35 return -ENOMEM;
36
37 spi->mode = SPI_MODE_0;
38
39 spi_set_drvdata(spi, wm831x);
40 wm831x->dev = &spi->dev;
41
42 wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
43 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);
47 return ret;
48 }
49
50 return wm831x_device_init(wm831x, type, spi->irq);
51}
52
53static int wm831x_spi_remove(struct spi_device *spi)
54{
55 struct wm831x *wm831x = spi_get_drvdata(spi);
56
57 wm831x_device_exit(wm831x);
58
59 return 0;
60}
61
62static int wm831x_spi_suspend(struct device *dev)
63{
64 struct wm831x *wm831x = dev_get_drvdata(dev);
65
66 return wm831x_device_suspend(wm831x);
67}
68
69static int wm831x_spi_poweroff(struct device *dev)
70{
71 struct wm831x *wm831x = dev_get_drvdata(dev);
72
73 wm831x_device_shutdown(wm831x);
74
75 return 0;
76}
77
78static const struct dev_pm_ops wm831x_spi_pm = {
79 .freeze = wm831x_spi_suspend,
80 .suspend = wm831x_spi_suspend,
81 .poweroff = wm831x_spi_poweroff,
82};
83
84static 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 { },
93};
94MODULE_DEVICE_TABLE(spi, wm831x_spi_ids);
95
96static struct spi_driver wm831x_spi_driver = {
97 .driver = {
98 .name = "wm831x",
99 .owner = THIS_MODULE,
100 .pm = &wm831x_spi_pm,
101 },
102 .id_table = wm831x_spi_ids,
103 .probe = wm831x_spi_probe,
104 .remove = wm831x_spi_remove,
105};
106
107static int __init wm831x_spi_init(void)
108{
109 int ret;
110
111 ret = spi_register_driver(&wm831x_spi_driver);
112 if (ret != 0)
113 pr_err("Failed to register WM831x SPI driver: %d\n", ret);
114
115 return 0;
116}
117subsys_initcall(wm831x_spi_init);
118
119static void __exit wm831x_spi_exit(void)
120{
121 spi_unregister_driver(&wm831x_spi_driver);
122}
123module_exit(wm831x_spi_exit);
124
125MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
126MODULE_LICENSE("GPL");
127MODULE_AUTHOR("Mark Brown");