blob: 22f7054d1b28633da6618d6ec7ab4158953876a8 [file] [log] [blame]
Mark Browne5b48682010-10-19 23:57:56 +02001/*
2 * wm831x-i2c.c -- I2C 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/i2c.h>
18#include <linux/delay.h>
19#include <linux/mfd/core.h>
20#include <linux/slab.h>
Mark Brown1df59812011-06-10 19:28:10 +010021#include <linux/err.h>
Charles Keepaxf6dd8442017-03-17 10:05:18 +000022#include <linux/of.h>
23#include <linux/of_device.h>
Mark Brown1df59812011-06-10 19:28:10 +010024#include <linux/regmap.h>
Mark Browne5b48682010-10-19 23:57:56 +020025
26#include <linux/mfd/wm831x/core.h>
27#include <linux/mfd/wm831x/pdata.h>
28
Mark Browne5b48682010-10-19 23:57:56 +020029static int wm831x_i2c_probe(struct i2c_client *i2c,
30 const struct i2c_device_id *id)
31{
Charles Keepaxf6dd8442017-03-17 10:05:18 +000032 struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
33 const struct of_device_id *of_id;
Mark Browne5b48682010-10-19 23:57:56 +020034 struct wm831x *wm831x;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000035 enum wm831x_parent type;
Mark Brown1df59812011-06-10 19:28:10 +010036 int ret;
Mark Browne5b48682010-10-19 23:57:56 +020037
Charles Keepaxf6dd8442017-03-17 10:05:18 +000038 if (i2c->dev.of_node) {
39 of_id = of_match_device(wm831x_of_match, &i2c->dev);
Gustavo A. R. Silva800e5452017-05-24 04:26:23 -050040 if (!of_id) {
41 dev_err(&i2c->dev, "Failed to match device\n");
42 return -ENODEV;
43 }
Charles Keepaxf6dd8442017-03-17 10:05:18 +000044 type = (enum wm831x_parent)of_id->data;
45 } else {
46 type = (enum wm831x_parent)id->driver_data;
47 }
48
Mark Brownd48cbb72011-10-25 14:47:40 +020049 wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
Mark Browne5b48682010-10-19 23:57:56 +020050 if (wm831x == NULL)
51 return -ENOMEM;
52
53 i2c_set_clientdata(i2c, wm831x);
54 wm831x->dev = &i2c->dev;
Charles Keepaxf6dd8442017-03-17 10:05:18 +000055 wm831x->type = type;
Mark Brown1df59812011-06-10 19:28:10 +010056
Mark Brown130a7032012-01-30 20:08:06 +000057 wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
Mark Brown1df59812011-06-10 19:28:10 +010058 if (IS_ERR(wm831x->regmap)) {
59 ret = PTR_ERR(wm831x->regmap);
60 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
61 ret);
Mark Brown1df59812011-06-10 19:28:10 +010062 return ret;
63 }
Mark Browne5b48682010-10-19 23:57:56 +020064
Charles Keepaxf6dd8442017-03-17 10:05:18 +000065 if (pdata)
66 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
67
68 return wm831x_device_init(wm831x, i2c->irq);
Mark Browne5b48682010-10-19 23:57:56 +020069}
70
71static int wm831x_i2c_remove(struct i2c_client *i2c)
72{
73 struct wm831x *wm831x = i2c_get_clientdata(i2c);
74
75 wm831x_device_exit(wm831x);
76
77 return 0;
78}
79
Mark Brownc538ddb2011-01-05 15:12:39 +000080static int wm831x_i2c_suspend(struct device *dev)
Mark Browne5b48682010-10-19 23:57:56 +020081{
Mark Brownc538ddb2011-01-05 15:12:39 +000082 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Browne5b48682010-10-19 23:57:56 +020083
84 return wm831x_device_suspend(wm831x);
85}
86
Mark Brownb9736a12013-11-08 18:51:25 +000087static int wm831x_i2c_poweroff(struct device *dev)
Mark Brown523d9cf2011-09-15 18:54:53 +020088{
Mark Brownb9736a12013-11-08 18:51:25 +000089 struct wm831x *wm831x = dev_get_drvdata(dev);
Mark Brown523d9cf2011-09-15 18:54:53 +020090
91 wm831x_device_shutdown(wm831x);
Mark Brownb9736a12013-11-08 18:51:25 +000092
93 return 0;
Mark Brown523d9cf2011-09-15 18:54:53 +020094}
95
Mark Browne5b48682010-10-19 23:57:56 +020096static const struct i2c_device_id wm831x_i2c_id[] = {
97 { "wm8310", WM8310 },
98 { "wm8311", WM8311 },
99 { "wm8312", WM8312 },
100 { "wm8320", WM8320 },
101 { "wm8321", WM8321 },
102 { "wm8325", WM8325 },
Mark Brown412dc112010-11-24 18:01:41 +0000103 { "wm8326", WM8326 },
Mark Browne5b48682010-10-19 23:57:56 +0200104 { }
105};
106MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
107
Mark Brownc538ddb2011-01-05 15:12:39 +0000108static const struct dev_pm_ops wm831x_pm_ops = {
109 .suspend = wm831x_i2c_suspend,
Mark Brownb9736a12013-11-08 18:51:25 +0000110 .poweroff = wm831x_i2c_poweroff,
Mark Brownc538ddb2011-01-05 15:12:39 +0000111};
Mark Browne5b48682010-10-19 23:57:56 +0200112
113static struct i2c_driver wm831x_i2c_driver = {
114 .driver = {
Mark Brownc538ddb2011-01-05 15:12:39 +0000115 .name = "wm831x",
Mark Brownc538ddb2011-01-05 15:12:39 +0000116 .pm = &wm831x_pm_ops,
Charles Keepaxf6dd8442017-03-17 10:05:18 +0000117 .of_match_table = of_match_ptr(wm831x_of_match),
Mark Browne5b48682010-10-19 23:57:56 +0200118 },
119 .probe = wm831x_i2c_probe,
120 .remove = wm831x_i2c_remove,
Mark Browne5b48682010-10-19 23:57:56 +0200121 .id_table = wm831x_i2c_id,
122};
123
124static int __init wm831x_i2c_init(void)
125{
126 int ret;
127
128 ret = i2c_add_driver(&wm831x_i2c_driver);
129 if (ret != 0)
130 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
131
132 return ret;
133}
134subsys_initcall(wm831x_i2c_init);
135
136static void __exit wm831x_i2c_exit(void)
137{
138 i2c_del_driver(&wm831x_i2c_driver);
139}
140module_exit(wm831x_i2c_exit);