Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 2 | /* |
| 3 | * wm831x-spi.c -- SPI access for Wolfson WM831x PMICs |
| 4 | * |
| 5 | * Copyright 2009,2010 Wolfson Microelectronics PLC. |
| 6 | * |
| 7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Paul Gortmaker | e85c5f0 | 2019-01-13 13:36:44 -0500 | [diff] [blame] | 11 | #include <linux/init.h> |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 12 | #include <linux/of.h> |
| 13 | #include <linux/of_device.h> |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 14 | #include <linux/pm.h> |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 15 | #include <linux/spi/spi.h> |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 16 | #include <linux/regmap.h> |
| 17 | #include <linux/err.h> |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 18 | |
| 19 | #include <linux/mfd/wm831x/core.h> |
| 20 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 21 | static int wm831x_spi_probe(struct spi_device *spi) |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 22 | { |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 23 | struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev); |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 24 | const struct spi_device_id *id = spi_get_device_id(spi); |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 25 | const struct of_device_id *of_id; |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 26 | struct wm831x *wm831x; |
| 27 | enum wm831x_parent type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 28 | int ret; |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 29 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 30 | if (spi->dev.of_node) { |
| 31 | of_id = of_match_device(wm831x_of_match, &spi->dev); |
Gustavo A. R. Silva | 7b55033 | 2017-05-24 04:18:52 -0500 | [diff] [blame] | 32 | if (!of_id) { |
| 33 | dev_err(&spi->dev, "Failed to match device\n"); |
| 34 | return -ENODEV; |
| 35 | } |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 36 | type = (enum wm831x_parent)of_id->data; |
| 37 | } else { |
| 38 | type = (enum wm831x_parent)id->driver_data; |
| 39 | } |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 40 | |
Mark Brown | d48cbb7 | 2011-10-25 14:47:40 +0200 | [diff] [blame] | 41 | wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 42 | if (wm831x == NULL) |
| 43 | return -ENOMEM; |
| 44 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 45 | spi->mode = SPI_MODE_0; |
| 46 | |
Jingoo Han | 0c0d2ab | 2013-04-06 15:42:48 +0900 | [diff] [blame] | 47 | spi_set_drvdata(spi, wm831x); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 48 | wm831x->dev = &spi->dev; |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 49 | wm831x->type = type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 50 | |
Mark Brown | 130a703 | 2012-01-30 20:08:06 +0000 | [diff] [blame] | 51 | wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 52 | if (IS_ERR(wm831x->regmap)) { |
| 53 | ret = PTR_ERR(wm831x->regmap); |
| 54 | dev_err(wm831x->dev, "Failed to allocate register map: %d\n", |
| 55 | ret); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 56 | return ret; |
| 57 | } |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 58 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 59 | if (pdata) |
| 60 | memcpy(&wm831x->pdata, pdata, sizeof(*pdata)); |
| 61 | |
| 62 | return wm831x_device_init(wm831x, spi->irq); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 65 | static int wm831x_spi_suspend(struct device *dev) |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 66 | { |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 67 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 68 | |
| 69 | return wm831x_device_suspend(wm831x); |
| 70 | } |
| 71 | |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 72 | static int wm831x_spi_poweroff(struct device *dev) |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 73 | { |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 74 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 75 | |
| 76 | wm831x_device_shutdown(wm831x); |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 77 | |
| 78 | return 0; |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 81 | static const struct dev_pm_ops wm831x_spi_pm = { |
| 82 | .freeze = wm831x_spi_suspend, |
| 83 | .suspend = wm831x_spi_suspend, |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 84 | .poweroff = wm831x_spi_poweroff, |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 87 | static const struct spi_device_id wm831x_spi_ids[] = { |
| 88 | { "wm8310", WM8310 }, |
| 89 | { "wm8311", WM8311 }, |
| 90 | { "wm8312", WM8312 }, |
| 91 | { "wm8320", WM8320 }, |
| 92 | { "wm8321", WM8321 }, |
| 93 | { "wm8325", WM8325 }, |
| 94 | { "wm8326", WM8326 }, |
| 95 | { }, |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 98 | static struct spi_driver wm831x_spi_driver = { |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 99 | .driver = { |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 100 | .name = "wm831x", |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 101 | .pm = &wm831x_spi_pm, |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 102 | .of_match_table = of_match_ptr(wm831x_of_match), |
Paul Gortmaker | e85c5f0 | 2019-01-13 13:36:44 -0500 | [diff] [blame] | 103 | .suppress_bind_attrs = true, |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 104 | }, |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 105 | .id_table = wm831x_spi_ids, |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 106 | .probe = wm831x_spi_probe, |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 109 | static int __init wm831x_spi_init(void) |
| 110 | { |
| 111 | int ret; |
| 112 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 113 | ret = spi_register_driver(&wm831x_spi_driver); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 114 | if (ret != 0) |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 115 | pr_err("Failed to register WM831x SPI driver: %d\n", ret); |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 116 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | subsys_initcall(wm831x_spi_init); |