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