treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[linux-block.git] / drivers / mfd / wm831x-i2c.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
e5b48684
MB
2/*
3 * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs
4 *
5 * Copyright 2009,2010 Wolfson Microelectronics PLC.
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
e5b48684
MB
8 */
9
10#include <linux/kernel.h>
af5db808 11#include <linux/init.h>
e5b48684
MB
12#include <linux/i2c.h>
13#include <linux/delay.h>
14#include <linux/mfd/core.h>
15#include <linux/slab.h>
1df5981b 16#include <linux/err.h>
f6dd8449
CK
17#include <linux/of.h>
18#include <linux/of_device.h>
1df5981b 19#include <linux/regmap.h>
e5b48684
MB
20
21#include <linux/mfd/wm831x/core.h>
22#include <linux/mfd/wm831x/pdata.h>
23
e5b48684
MB
24static int wm831x_i2c_probe(struct i2c_client *i2c,
25 const struct i2c_device_id *id)
26{
f6dd8449
CK
27 struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
28 const struct of_device_id *of_id;
e5b48684 29 struct wm831x *wm831x;
f6dd8449 30 enum wm831x_parent type;
1df5981b 31 int ret;
e5b48684 32
f6dd8449
CK
33 if (i2c->dev.of_node) {
34 of_id = of_match_device(wm831x_of_match, &i2c->dev);
800e5455
GS
35 if (!of_id) {
36 dev_err(&i2c->dev, "Failed to match device\n");
37 return -ENODEV;
38 }
f6dd8449
CK
39 type = (enum wm831x_parent)of_id->data;
40 } else {
41 type = (enum wm831x_parent)id->driver_data;
42 }
43
d48cbb74 44 wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
e5b48684
MB
45 if (wm831x == NULL)
46 return -ENOMEM;
47
48 i2c_set_clientdata(i2c, wm831x);
49 wm831x->dev = &i2c->dev;
f6dd8449 50 wm831x->type = type;
1df5981b 51
130a7032 52 wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
1df5981b
MB
53 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);
1df5981b
MB
57 return ret;
58 }
e5b48684 59
f6dd8449
CK
60 if (pdata)
61 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
62
63 return wm831x_device_init(wm831x, i2c->irq);
e5b48684
MB
64}
65
c538ddbe 66static int wm831x_i2c_suspend(struct device *dev)
e5b48684 67{
c538ddbe 68 struct wm831x *wm831x = dev_get_drvdata(dev);
e5b48684
MB
69
70 return wm831x_device_suspend(wm831x);
71}
72
b9736a16 73static int wm831x_i2c_poweroff(struct device *dev)
523d9cfb 74{
b9736a16 75 struct wm831x *wm831x = dev_get_drvdata(dev);
523d9cfb
MB
76
77 wm831x_device_shutdown(wm831x);
b9736a16
MB
78
79 return 0;
523d9cfb
MB
80}
81
e5b48684
MB
82static const struct i2c_device_id wm831x_i2c_id[] = {
83 { "wm8310", WM8310 },
84 { "wm8311", WM8311 },
85 { "wm8312", WM8312 },
86 { "wm8320", WM8320 },
87 { "wm8321", WM8321 },
88 { "wm8325", WM8325 },
412dc11d 89 { "wm8326", WM8326 },
e5b48684
MB
90 { }
91};
e5b48684 92
c538ddbe
MB
93static const struct dev_pm_ops wm831x_pm_ops = {
94 .suspend = wm831x_i2c_suspend,
b9736a16 95 .poweroff = wm831x_i2c_poweroff,
c538ddbe 96};
e5b48684
MB
97
98static struct i2c_driver wm831x_i2c_driver = {
99 .driver = {
c538ddbe 100 .name = "wm831x",
c538ddbe 101 .pm = &wm831x_pm_ops,
f6dd8449 102 .of_match_table = of_match_ptr(wm831x_of_match),
af5db808 103 .suppress_bind_attrs = true,
e5b48684
MB
104 },
105 .probe = wm831x_i2c_probe,
e5b48684
MB
106 .id_table = wm831x_i2c_id,
107};
108
109static int __init wm831x_i2c_init(void)
110{
111 int ret;
112
113 ret = i2c_add_driver(&wm831x_i2c_driver);
114 if (ret != 0)
115 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
116
117 return ret;
118}
119subsys_initcall(wm831x_i2c_init);