Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * wm831x-spi.c -- SPI 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> |
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 | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 19 | #include <linux/pm.h> |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 20 | #include <linux/spi/spi.h> |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 21 | #include <linux/regmap.h> |
| 22 | #include <linux/err.h> |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 23 | |
| 24 | #include <linux/mfd/wm831x/core.h> |
| 25 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 26 | static int wm831x_spi_probe(struct spi_device *spi) |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 27 | { |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 28 | struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev); |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 29 | const struct spi_device_id *id = spi_get_device_id(spi); |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 30 | const struct of_device_id *of_id; |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 31 | struct wm831x *wm831x; |
| 32 | enum wm831x_parent type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 33 | int ret; |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 34 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 35 | if (spi->dev.of_node) { |
| 36 | of_id = of_match_device(wm831x_of_match, &spi->dev); |
Gustavo A. R. Silva | 7b55033 | 2017-05-24 04:18:52 -0500 | [diff] [blame] | 37 | if (!of_id) { |
| 38 | dev_err(&spi->dev, "Failed to match device\n"); |
| 39 | return -ENODEV; |
| 40 | } |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 41 | type = (enum wm831x_parent)of_id->data; |
| 42 | } else { |
| 43 | type = (enum wm831x_parent)id->driver_data; |
| 44 | } |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 45 | |
Mark Brown | d48cbb7 | 2011-10-25 14:47:40 +0200 | [diff] [blame] | 46 | wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 47 | if (wm831x == NULL) |
| 48 | return -ENOMEM; |
| 49 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 50 | spi->mode = SPI_MODE_0; |
| 51 | |
Jingoo Han | 0c0d2ab | 2013-04-06 15:42:48 +0900 | [diff] [blame] | 52 | spi_set_drvdata(spi, wm831x); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 53 | wm831x->dev = &spi->dev; |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 54 | wm831x->type = type; |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 55 | |
Mark Brown | 130a703 | 2012-01-30 20:08:06 +0000 | [diff] [blame] | 56 | wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 57 | if (IS_ERR(wm831x->regmap)) { |
| 58 | ret = PTR_ERR(wm831x->regmap); |
| 59 | dev_err(wm831x->dev, "Failed to allocate register map: %d\n", |
| 60 | ret); |
Mark Brown | 1df5981 | 2011-06-10 19:28:10 +0100 | [diff] [blame] | 61 | return ret; |
| 62 | } |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 63 | |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 64 | if (pdata) |
| 65 | memcpy(&wm831x->pdata, pdata, sizeof(*pdata)); |
| 66 | |
| 67 | return wm831x_device_init(wm831x, spi->irq); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 70 | static int wm831x_spi_remove(struct spi_device *spi) |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 71 | { |
Jingoo Han | 0c0d2ab | 2013-04-06 15:42:48 +0900 | [diff] [blame] | 72 | struct wm831x *wm831x = spi_get_drvdata(spi); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 73 | |
| 74 | wm831x_device_exit(wm831x); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 79 | static int wm831x_spi_suspend(struct device *dev) |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 80 | { |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 81 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 82 | |
| 83 | return wm831x_device_suspend(wm831x); |
| 84 | } |
| 85 | |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 86 | static int wm831x_spi_poweroff(struct device *dev) |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 87 | { |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 88 | struct wm831x *wm831x = dev_get_drvdata(dev); |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 89 | |
| 90 | wm831x_device_shutdown(wm831x); |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 91 | |
| 92 | return 0; |
Mark Brown | 523d9cf | 2011-09-15 18:54:53 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 95 | static const struct dev_pm_ops wm831x_spi_pm = { |
| 96 | .freeze = wm831x_spi_suspend, |
| 97 | .suspend = wm831x_spi_suspend, |
Mark Brown | b9736a1 | 2013-11-08 18:51:25 +0000 | [diff] [blame] | 98 | .poweroff = wm831x_spi_poweroff, |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 101 | static const struct spi_device_id wm831x_spi_ids[] = { |
| 102 | { "wm8310", WM8310 }, |
| 103 | { "wm8311", WM8311 }, |
| 104 | { "wm8312", WM8312 }, |
| 105 | { "wm8320", WM8320 }, |
| 106 | { "wm8321", WM8321 }, |
| 107 | { "wm8325", WM8325 }, |
| 108 | { "wm8326", WM8326 }, |
| 109 | { }, |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 110 | }; |
Hai Dong | fe2afaa | 2012-02-24 16:03:00 -0600 | [diff] [blame] | 111 | MODULE_DEVICE_TABLE(spi, wm831x_spi_ids); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 112 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 113 | static struct spi_driver wm831x_spi_driver = { |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 114 | .driver = { |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 115 | .name = "wm831x", |
Mark Brown | 87d1906 | 2011-02-01 11:46:12 +0000 | [diff] [blame] | 116 | .pm = &wm831x_spi_pm, |
Charles Keepax | f6dd844 | 2017-03-17 10:05:18 +0000 | [diff] [blame] | 117 | .of_match_table = of_match_ptr(wm831x_of_match), |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 118 | }, |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 119 | .id_table = wm831x_spi_ids, |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 120 | .probe = wm831x_spi_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 121 | .remove = wm831x_spi_remove, |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 124 | static int __init wm831x_spi_init(void) |
| 125 | { |
| 126 | int ret; |
| 127 | |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 128 | ret = spi_register_driver(&wm831x_spi_driver); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 129 | if (ret != 0) |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 130 | pr_err("Failed to register WM831x SPI driver: %d\n", ret); |
Mark Brown | 412dc11 | 2010-11-24 18:01:41 +0000 | [diff] [blame] | 131 | |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | subsys_initcall(wm831x_spi_init); |
| 135 | |
| 136 | static void __exit wm831x_spi_exit(void) |
| 137 | { |
Mark Brown | 5570c2f | 2011-07-24 11:44:19 +0100 | [diff] [blame] | 138 | spi_unregister_driver(&wm831x_spi_driver); |
Mark Brown | 2aa13b9 | 2010-10-20 00:00:11 +0200 | [diff] [blame] | 139 | } |
| 140 | module_exit(wm831x_spi_exit); |
| 141 | |
| 142 | MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs"); |
| 143 | MODULE_LICENSE("GPL"); |
| 144 | MODULE_AUTHOR("Mark Brown"); |