1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs
5 * Copyright 2009,2010 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/delay.h>
14 #include <linux/mfd/core.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
21 #include <linux/mfd/wm831x/core.h>
22 #include <linux/mfd/wm831x/pdata.h>
24 static int wm831x_i2c_probe(struct i2c_client
*i2c
,
25 const struct i2c_device_id
*id
)
27 struct wm831x_pdata
*pdata
= dev_get_platdata(&i2c
->dev
);
28 const struct of_device_id
*of_id
;
29 struct wm831x
*wm831x
;
30 enum wm831x_parent type
;
33 if (i2c
->dev
.of_node
) {
34 of_id
= of_match_device(wm831x_of_match
, &i2c
->dev
);
36 dev_err(&i2c
->dev
, "Failed to match device\n");
39 type
= (enum wm831x_parent
)of_id
->data
;
41 type
= (enum wm831x_parent
)id
->driver_data
;
44 wm831x
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm831x
), GFP_KERNEL
);
48 i2c_set_clientdata(i2c
, wm831x
);
49 wm831x
->dev
= &i2c
->dev
;
52 wm831x
->regmap
= devm_regmap_init_i2c(i2c
, &wm831x_regmap_config
);
53 if (IS_ERR(wm831x
->regmap
)) {
54 ret
= PTR_ERR(wm831x
->regmap
);
55 dev_err(wm831x
->dev
, "Failed to allocate register map: %d\n",
61 memcpy(&wm831x
->pdata
, pdata
, sizeof(*pdata
));
63 return wm831x_device_init(wm831x
, i2c
->irq
);
66 static int wm831x_i2c_suspend(struct device
*dev
)
68 struct wm831x
*wm831x
= dev_get_drvdata(dev
);
70 return wm831x_device_suspend(wm831x
);
73 static int wm831x_i2c_poweroff(struct device
*dev
)
75 struct wm831x
*wm831x
= dev_get_drvdata(dev
);
77 wm831x_device_shutdown(wm831x
);
82 static const struct i2c_device_id wm831x_i2c_id
[] = {
93 static const struct dev_pm_ops wm831x_pm_ops
= {
94 .suspend
= wm831x_i2c_suspend
,
95 .poweroff
= wm831x_i2c_poweroff
,
98 static struct i2c_driver wm831x_i2c_driver
= {
101 .pm
= &wm831x_pm_ops
,
102 .of_match_table
= of_match_ptr(wm831x_of_match
),
103 .suppress_bind_attrs
= true,
105 .probe
= wm831x_i2c_probe
,
106 .id_table
= wm831x_i2c_id
,
109 static int __init
wm831x_i2c_init(void)
113 ret
= i2c_add_driver(&wm831x_i2c_driver
);
115 pr_err("Failed to register wm831x I2C driver: %d\n", ret
);
119 subsys_initcall(wm831x_i2c_init
);