xref: /openbmc/linux/drivers/mfd/madera-spi.c (revision 045f77ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SPI bus interface to Cirrus Logic Madera codecs
4  *
5  * Copyright (C) 2015-2018 Cirrus Logic
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; version 2.
10  */
11 
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
19 
20 #include <linux/mfd/madera/core.h>
21 
22 #include "madera.h"
23 
24 static int madera_spi_probe(struct spi_device *spi)
25 {
26 	const struct spi_device_id *id = spi_get_device_id(spi);
27 	struct madera *madera;
28 	const struct regmap_config *regmap_16bit_config = NULL;
29 	const struct regmap_config *regmap_32bit_config = NULL;
30 	const void *of_data;
31 	unsigned long type;
32 	const char *name;
33 	int ret;
34 
35 	of_data = of_device_get_match_data(&spi->dev);
36 	if (of_data)
37 		type = (unsigned long)of_data;
38 	else
39 		type = id->driver_data;
40 
41 	switch (type) {
42 	case CS47L35:
43 		if (IS_ENABLED(CONFIG_MFD_CS47L35)) {
44 			regmap_16bit_config = &cs47l35_16bit_spi_regmap;
45 			regmap_32bit_config = &cs47l35_32bit_spi_regmap;
46 		}
47 		break;
48 	case CS47L85:
49 	case WM1840:
50 		if (IS_ENABLED(CONFIG_MFD_CS47L85)) {
51 			regmap_16bit_config = &cs47l85_16bit_spi_regmap;
52 			regmap_32bit_config = &cs47l85_32bit_spi_regmap;
53 		}
54 		break;
55 	case CS47L90:
56 	case CS47L91:
57 		if (IS_ENABLED(CONFIG_MFD_CS47L90)) {
58 			regmap_16bit_config = &cs47l90_16bit_spi_regmap;
59 			regmap_32bit_config = &cs47l90_32bit_spi_regmap;
60 		}
61 		break;
62 	default:
63 		dev_err(&spi->dev,
64 			"Unknown Madera SPI device type %ld\n", type);
65 		return -EINVAL;
66 	}
67 
68 	name = madera_name_from_type(type);
69 
70 	if (!regmap_16bit_config) {
71 		/* it's polite to say which codec isn't built into the kernel */
72 		dev_err(&spi->dev,
73 			"Kernel does not include support for %s\n", name);
74 		return -EINVAL;
75 	}
76 
77 	madera = devm_kzalloc(&spi->dev, sizeof(*madera), GFP_KERNEL);
78 	if (!madera)
79 		return -ENOMEM;
80 
81 	madera->regmap = devm_regmap_init_spi(spi, regmap_16bit_config);
82 	if (IS_ERR(madera->regmap)) {
83 		ret = PTR_ERR(madera->regmap);
84 		dev_err(&spi->dev,
85 			"Failed to allocate 16-bit register map: %d\n",	ret);
86 		return ret;
87 	}
88 
89 	madera->regmap_32bit = devm_regmap_init_spi(spi, regmap_32bit_config);
90 	if (IS_ERR(madera->regmap_32bit)) {
91 		ret = PTR_ERR(madera->regmap_32bit);
92 		dev_err(&spi->dev,
93 			"Failed to allocate 32-bit register map: %d\n",	ret);
94 		return ret;
95 	}
96 
97 	madera->type = type;
98 	madera->type_name = name;
99 	madera->dev = &spi->dev;
100 	madera->irq = spi->irq;
101 
102 	return madera_dev_init(madera);
103 }
104 
105 static int madera_spi_remove(struct spi_device *spi)
106 {
107 	struct madera *madera = spi_get_drvdata(spi);
108 
109 	madera_dev_exit(madera);
110 
111 	return 0;
112 }
113 
114 static const struct spi_device_id madera_spi_ids[] = {
115 	{ "cs47l35", CS47L35 },
116 	{ "cs47l85", CS47L85 },
117 	{ "cs47l90", CS47L90 },
118 	{ "cs47l91", CS47L91 },
119 	{ "wm1840", WM1840 },
120 	{ }
121 };
122 MODULE_DEVICE_TABLE(spi, madera_spi_ids);
123 
124 static struct spi_driver madera_spi_driver = {
125 	.driver = {
126 		.name	= "madera",
127 		.pm	= &madera_pm_ops,
128 		.of_match_table	= of_match_ptr(madera_of_match),
129 	},
130 	.probe		= madera_spi_probe,
131 	.remove		= madera_spi_remove,
132 	.id_table	= madera_spi_ids,
133 };
134 
135 module_spi_driver(madera_spi_driver);
136 
137 MODULE_DESCRIPTION("Madera SPI bus interface");
138 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
139 MODULE_LICENSE("GPL v2");
140