Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 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> |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Paul Gortmaker | af5db808 | 2019-01-13 13:36:45 -0500 | [diff] [blame] | 11 | #include <linux/init.h> |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 12 | #include <linux/i2c.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/mfd/core.h> |
| 15 | #include <linux/slab.h> |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 16 | #include <linux/err.h> |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 20 | |
| 21 | #include <linux/mfd/wm831x/core.h> |
| 22 | #include <linux/mfd/wm831x/pdata.h> |
| 23 | |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 24 | static int wm831x_i2c_probe(struct i2c_client *i2c, |
| 25 | const struct i2c_device_id *id) |
| 26 | { |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 27 | struct wm831x_pdata *pdata = dev_get_platdata(&i2c->dev); |
| 28 | const struct of_device_id *of_id; |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 29 | struct wm831x *wm831x; |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 30 | enum wm831x_parent type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 31 | int ret; |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 32 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 33 | if (i2c->dev.of_node) { |
| 34 | of_id = of_match_device(wm831x_of_match, &i2c->dev); |
Gustavo A. R. Silva | 800e545 | 2017-05-24 04:26:23 -0500 | [diff] [blame] | 35 | if (!of_id) { |
| 36 | dev_err(&i2c->dev, "Failed to match device\n"); |
| 37 | return -ENODEV; |
| 38 | } |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 39 | type = (enum wm831x_parent)of_id->data; |
| 40 | } else { |
| 41 | type = (enum wm831x_parent)id->driver_data; |
| 42 | } |
| 43 | |
Mark Brown | d48cbb7 | 2011-10-25 14:47:40 +0200 | [diff] [blame] | 44 | wm831x = devm_kzalloc(&i2c->dev, sizeof(struct wm831x), GFP_KERNEL); |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 45 | if (wm831x == NULL) |
| 46 | return -ENOMEM; |
| 47 | |
| 48 | i2c_set_clientdata(i2c, wm831x); |
| 49 | wm831x->dev = &i2c->dev; |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 50 | wm831x->type = type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 51 | |
Mark Brown | 130a703 | 2012-01-30 20:08:06 +0000 | [diff] [blame] | 52 | wm831x->regmap = devm_regmap_init_i2c(i2c, &wm831x_regmap_config); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 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); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 57 | return ret; |
| 58 | } |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 59 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 60 | if (pdata) |
| 61 | memcpy(&wm831x->pdata, pdata, sizeof(*pdata)); |
| 62 | |
| 63 | return wm831x_device_init(wm831x, i2c->irq); |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 66 | static int wm831x_i2c_suspend(struct device *dev) |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 67 | { |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 68 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 69 | |
| 70 | return wm831x_device_suspend(wm831x); |
| 71 | } |
| 72 | |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 73 | static int wm831x_i2c_poweroff(struct device *dev) |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 74 | { |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 75 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 76 | |
| 77 | wm831x_device_shutdown(wm831x); |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 78 | |
| 79 | return 0; |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 82 | static 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 Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 89 | { "wm8326", WM8326 }, |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 90 | { } |
| 91 | }; |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 92 | |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 93 | static const struct dev_pm_ops wm831x_pm_ops = { |
| 94 | .suspend = wm831x_i2c_suspend, |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 95 | .poweroff = wm831x_i2c_poweroff, |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 96 | }; |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 97 | |
| 98 | static struct i2c_driver wm831x_i2c_driver = { |
| 99 | .driver = { |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 100 | .name = "wm831x", |
Mark Brown | c538ddb | 2011-01-05 15:12:39 +0000 | [diff] [blame] | 101 | .pm = &wm831x_pm_ops, |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 102 | .of_match_table = of_match_ptr(wm831x_of_match), |
Paul Gortmaker | af5db808 | 2019-01-13 13:36:45 -0500 | [diff] [blame] | 103 | .suppress_bind_attrs = true, |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 104 | }, |
| 105 | .probe = wm831x_i2c_probe, |
Mark Brown | e5b4868 | 2010-10-19 23:57:56 +0200 | [diff] [blame] | 106 | .id_table = wm831x_i2c_id, |
| 107 | }; |
| 108 | |
| 109 | static 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 | } |
| 119 | subsys_initcall(wm831x_i2c_init); |