0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/init.h>
0012 #include <linux/i2c.h>
0013 #include <linux/delay.h>
0014 #include <linux/mfd/core.h>
0015 #include <linux/slab.h>
0016 #include <linux/err.h>
0017 #include <linux/of.h>
0018 #include <linux/of_device.h>
0019 #include <linux/regmap.h>
0020
0021 #include <linux/mfd/wm831x/core.h>
0022 #include <linux/mfd/wm831x/pdata.h>
0023
0024 static int wm831x_i2c_probe(struct i2c_client *i2c,
0025 const struct i2c_device_id *id)
0026 {
0027 struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev);
0028 const struct of_device_id *of_id;
0029 struct wm831x *wm831x;
0030 enum wm831x_parent type;
0031 int ret;
0032
0033 if (i2c->dev.of_node) {
0034 of_id = of_match_device(wm831x_of_match, &i2c->dev);
0035 if (!of_id) {
0036 dev_err(&i2c->dev, "Failed to match device\n");
0037 return -ENODEV;
0038 }
0039 type = (enum wm831x_parent)of_id->data;
0040 } else {
0041 type = (enum wm831x_parent)id->driver_data;
0042 }
0043
0044 wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL);
0045 if (wm831x == NULL)
0046 return -ENOMEM;
0047
0048 i2c_set_clientdata(i2c, wm831x);
0049 wm831x->dev = &i2c->dev;
0050 wm831x->type = type;
0051
0052 wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config);
0053 if (IS_ERR(wm831x->regmap)) {
0054 ret = PTR_ERR(wm831x->regmap);
0055 dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
0056 ret);
0057 return ret;
0058 }
0059
0060 if (pdata)
0061 memcpy(&wm831x->pdata, pdata, sizeof(*pdata));
0062
0063 return wm831x_device_init(wm831x, i2c->irq);
0064 }
0065
0066 static int wm831x_i2c_suspend(struct device *dev)
0067 {
0068 struct wm831x *wm831x = dev_get_drvdata(dev);
0069
0070 return wm831x_device_suspend(wm831x);
0071 }
0072
0073 static int wm831x_i2c_poweroff(struct device *dev)
0074 {
0075 struct wm831x *wm831x = dev_get_drvdata(dev);
0076
0077 wm831x_device_shutdown(wm831x);
0078
0079 return 0;
0080 }
0081
0082 static const struct i2c_device_id wm831x_i2c_id[] = {
0083 { "wm8310", WM8310 },
0084 { "wm8311", WM8311 },
0085 { "wm8312", WM8312 },
0086 { "wm8320", WM8320 },
0087 { "wm8321", WM8321 },
0088 { "wm8325", WM8325 },
0089 { "wm8326", WM8326 },
0090 { }
0091 };
0092
0093 static const struct dev_pm_ops wm831x_pm_ops = {
0094 .suspend = wm831x_i2c_suspend,
0095 .poweroff = wm831x_i2c_poweroff,
0096 };
0097
0098 static struct i2c_driver wm831x_i2c_driver = {
0099 .driver = {
0100 .name = "wm831x",
0101 .pm = &wm831x_pm_ops,
0102 .of_match_table = of_match_ptr(wm831x_of_match),
0103 .suppress_bind_attrs = true,
0104 },
0105 .probe = wm831x_i2c_probe,
0106 .id_table = wm831x_i2c_id,
0107 };
0108
0109 static int __init wm831x_i2c_init(void)
0110 {
0111 int ret;
0112
0113 ret = i2c_add_driver(&wm831x_i2c_driver);
0114 if (ret != 0)
0115 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
0116
0117 return ret;
0118 }
0119 subsys_initcall(wm831x_i2c_init);