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