blob: daa1ad036595496c427a1ab3bcef47c04336d6df [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mark Browne5b48682010-10-19 23:57:56 +02002/*
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>
Mark Browne5b48682010-10-19 23:57:56 +02008 */
9
10#include <linux/kernel.h>
Paul Gortmakeraf5db8082019-01-13 13:36:45 -050011#include <linux/init.h>
Mark Browne5b48682010-10-19 23:57:56 +020012#include <linux/i2c.h>
13#include <linux/delay.h>
14#include <linux/mfd/core.h>
15#include <linux/slab.h>
Mark Brown1df59812011-06-10 19:28:10 +010016#include <linux/err.h>
Charles Keepaxf6dd8442017-03-17 10:05:18 +000017#include <linux/of.h>
18#include <linux/of_device.h>
Mark Brown1df59812011-06-10 19:28:10 +010019#include <linux/regmap.h>
Mark Browne5b48682010-10-19 23:57:56 +020020
21#include <linux/mfd/wm831x/core.h>
22#include <linux/mfd/wm831x/pdata.h>
23
Mark Browne5b48682010-10-19 23:57:56 +020024static int wm831x_i2c_probe(struct i2c_client *i2c,
25 const struct i2c_device_id *id)
26{
Charles Keepaxf6dd8442017-03-17 10:05:18 +000027 struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
28 const struct of_device_id *of_id;
Mark Browne5b48682010-10-19 23:57:56 +020029 struct wm831x *wm831x;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000030 enum wm831x_parent type;
Mark Brown1df59812011-06-10 19:28:10 +010031 int ret;
Mark Browne5b48682010-10-19 23:57:56 +020032
Charles Keepaxf6dd8442017-03-17 10:05:18 +000033 if (i2c->dev.of_node) {
34 of_id = of_match_device(wm831x_of_match, &i2c->dev);
Gustavo A. R. Silva800e5452017-05-24 04:26:23 -050035 if (!of_id) {
36 dev_err(&i2c->dev, "Failed to match device\n");
37 return -ENODEV;
38 }
Charles Keepaxf6dd8442017-03-17 10:05:18 +000039 type = (enum wm831x_parent)of_id->data;
40 } else {
41 type = (enum wm831x_parent)id->driver_data;
42 }
43
Mark Brownd48cbb72011-10-25 14:47:40 +020044 wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
Mark Browne5b48682010-10-19 23:57:56 +020045 if (wm831x == NULL)
46 return -ENOMEM;
47
48 i2c_set_clientdata(i2c, wm831x);
49 wm831x->dev = &i2c->dev;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000050 wm831x->type = type;
Mark Brown1df59812011-06-10 19:28:10 +010051
Mark Brown130a7032012-01-30 20:08:06 +000052 wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010053 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);
Mark Brown1df59812011-06-10 19:28:10 +010057 return ret;
58 }
Mark Browne5b48682010-10-19 23:57:56 +020059
Charles Keepaxf6dd8442017-03-17 10:05:18 +000060 if (pdata)
61 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
62
63 return wm831x_device_init(wm831x, i2c->irq);
Mark Browne5b48682010-10-19 23:57:56 +020064}
65
Mark Brownc538ddb2011-01-05 15:12:39 +000066static int wm831x_i2c_suspend(struct device *dev)
Mark Browne5b48682010-10-19 23:57:56 +020067{
Mark Brownc538ddb2011-01-05 15:12:39 +000068 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Browne5b48682010-10-19 23:57:56 +020069
70 return wm831x_device_suspend(wm831x);
71}
72
Mark Brownb9736a12013-11-08 18:51:25 +000073static int wm831x_i2c_poweroff(struct device *dev)
Mark Brown523d9cf2011-09-15 18:54:53 +020074{
Mark Brownb9736a12013-11-08 18:51:25 +000075 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown523d9cf2011-09-15 18:54:53 +020076
77 wm831x_device_shutdown(wm831x);
Mark Brownb9736a12013-11-08 18:51:25 +000078
79 return 0;
Mark Brown523d9cf2011-09-15 18:54:53 +020080}
81
Mark Browne5b48682010-10-19 23:57:56 +020082static 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 },
Mark Brown412dc112010-11-24 18:01:41 +000089 { "wm8326", WM8326 },
Mark Browne5b48682010-10-19 23:57:56 +020090 { }
91};
Mark Browne5b48682010-10-19 23:57:56 +020092
Mark Brownc538ddb2011-01-05 15:12:39 +000093static const struct dev_pm_ops wm831x_pm_ops = {
94 .suspend = wm831x_i2c_suspend,
Mark Brownb9736a12013-11-08 18:51:25 +000095 .poweroff = wm831x_i2c_poweroff,
Mark Brownc538ddb2011-01-05 15:12:39 +000096};
Mark Browne5b48682010-10-19 23:57:56 +020097
98static struct i2c_driver wm831x_i2c_driver = {
99 .driver = {
Mark Brownc538ddb2011-01-05 15:12:39 +0000100 .name = "wm831x",
Mark Brownc538ddb2011-01-05 15:12:39 +0000101 .pm = &wm831x_pm_ops,
Charles Keepaxf6dd8442017-03-17 10:05:18 +0000102 .of_match_table = of_match_ptr(wm831x_of_match),
Paul Gortmakeraf5db8082019-01-13 13:36:45 -0500103 .suppress_bind_attrs = true,
Mark Browne5b48682010-10-19 23:57:56 +0200104 },
105 .probe = wm831x_i2c_probe,
Mark Browne5b48682010-10-19 23:57:56 +0200106 .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);