wm831x-spi.c 3.2 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * wm831x-spi.c  --  SPI access for Wolfson WM831x PMICs
 *
 * Copyright 2009,2010 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
17 18
#include <linux/of.h>
#include <linux/of_device.h>
19
#include <linux/pm.h>
M
Mark Brown 已提交
20
#include <linux/spi/spi.h>
21 22
#include <linux/regmap.h>
#include <linux/err.h>
M
Mark Brown 已提交
23 24 25

#include <linux/mfd/wm831x/core.h>

B
Bill Pemberton 已提交
26
static int wm831x_spi_probe(struct spi_device *spi)
M
Mark Brown 已提交
27
{
28
	struct wm831x_pdata *pdata = dev_get_platdata(&spi->dev);
29
	const struct spi_device_id *id = spi_get_device_id(spi);
30
	const struct of_device_id *of_id;
M
Mark Brown 已提交
31 32
	struct wm831x *wm831x;
	enum wm831x_parent type;
33
	int ret;
M
Mark Brown 已提交
34

35 36 37 38 39 40
	if (spi->dev.of_node) {
		of_id = of_match_device(wm831x_of_match, &spi->dev);
		type = (enum wm831x_parent)of_id->data;
	} else {
		type = (enum wm831x_parent)id->driver_data;
	}
M
Mark Brown 已提交
41

42
	wm831x = devm_kzalloc(&spi->dev, sizeof(struct wm831x), GFP_KERNEL);
M
Mark Brown 已提交
43 44 45 46 47
	if (wm831x == NULL)
		return -ENOMEM;

	spi->mode = SPI_MODE_0;

48
	spi_set_drvdata(spi, wm831x);
M
Mark Brown 已提交
49
	wm831x->dev = &spi->dev;
50
	wm831x->type = type;
51

52
	wm831x->regmap = devm_regmap_init_spi(spi, &wm831x_regmap_config);
53 54 55 56 57 58
	if (IS_ERR(wm831x->regmap)) {
		ret = PTR_ERR(wm831x->regmap);
		dev_err(wm831x->dev, "Failed to allocate register map: %d\n",
			ret);
		return ret;
	}
M
Mark Brown 已提交
59

60 61 62 63
	if (pdata)
		memcpy(&wm831x->pdata, pdata, sizeof(*pdata));

	return wm831x_device_init(wm831x, spi->irq);
M
Mark Brown 已提交
64 65
}

B
Bill Pemberton 已提交
66
static int wm831x_spi_remove(struct spi_device *spi)
M
Mark Brown 已提交
67
{
68
	struct wm831x *wm831x = spi_get_drvdata(spi);
M
Mark Brown 已提交
69 70 71 72 73 74

	wm831x_device_exit(wm831x);

	return 0;
}

75
static int wm831x_spi_suspend(struct device *dev)
M
Mark Brown 已提交
76
{
77
	struct wm831x *wm831x = dev_get_drvdata(dev);
M
Mark Brown 已提交
78 79 80 81

	return wm831x_device_suspend(wm831x);
}

82
static int wm831x_spi_poweroff(struct device *dev)
83
{
84
	struct wm831x *wm831x = dev_get_drvdata(dev);
85 86

	wm831x_device_shutdown(wm831x);
87 88

	return 0;
89 90
}

91 92 93
static const struct dev_pm_ops wm831x_spi_pm = {
	.freeze = wm831x_spi_suspend,
	.suspend = wm831x_spi_suspend,
94
	.poweroff = wm831x_spi_poweroff,
95 96
};

97 98 99 100 101 102 103 104 105
static const struct spi_device_id wm831x_spi_ids[] = {
	{ "wm8310", WM8310 },
	{ "wm8311", WM8311 },
	{ "wm8312", WM8312 },
	{ "wm8320", WM8320 },
	{ "wm8321", WM8321 },
	{ "wm8325", WM8325 },
	{ "wm8326", WM8326 },
	{ },
M
Mark Brown 已提交
106
};
107
MODULE_DEVICE_TABLE(spi, wm831x_spi_ids);
M
Mark Brown 已提交
108

109
static struct spi_driver wm831x_spi_driver = {
M
Mark Brown 已提交
110
	.driver = {
111
		.name	= "wm831x",
112
		.pm	= &wm831x_spi_pm,
113
		.of_match_table = of_match_ptr(wm831x_of_match),
M
Mark Brown 已提交
114
	},
115
	.id_table	= wm831x_spi_ids,
M
Mark Brown 已提交
116
	.probe		= wm831x_spi_probe,
B
Bill Pemberton 已提交
117
	.remove		= wm831x_spi_remove,
M
Mark Brown 已提交
118 119
};

M
Mark Brown 已提交
120 121 122 123
static int __init wm831x_spi_init(void)
{
	int ret;

124
	ret = spi_register_driver(&wm831x_spi_driver);
M
Mark Brown 已提交
125
	if (ret != 0)
126
		pr_err("Failed to register WM831x SPI driver: %d\n", ret);
M
Mark Brown 已提交
127

M
Mark Brown 已提交
128 129 130 131 132 133
	return 0;
}
subsys_initcall(wm831x_spi_init);

static void __exit wm831x_spi_exit(void)
{
134
	spi_unregister_driver(&wm831x_spi_driver);
M
Mark Brown 已提交
135 136 137 138 139 140
}
module_exit(wm831x_spi_exit);

MODULE_DESCRIPTION("SPI support for WM831x/2x AudioPlus PMICs");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mark Brown");