xref: /openbmc/linux/sound/soc/codecs/cs35l41-spi.c (revision ee8ec048)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // cs35l41-spi.c -- CS35l41 SPI driver
4 //
5 // Copyright 2017-2021 Cirrus Logic, Inc.
6 //
7 // Author: David Rhodes	<david.rhodes@cirrus.com>
8 
9 #include <linux/acpi.h>
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
17 
18 #include <sound/cs35l41.h>
19 #include "cs35l41.h"
20 
21 static struct regmap_config cs35l41_regmap_spi = {
22 	.reg_bits = 32,
23 	.val_bits = 32,
24 	.pad_bits = 16,
25 	.reg_stride = CS35L41_REGSTRIDE,
26 	.reg_format_endian = REGMAP_ENDIAN_BIG,
27 	.val_format_endian = REGMAP_ENDIAN_BIG,
28 	.max_register = CS35L41_LASTREG,
29 	.reg_defaults = cs35l41_reg,
30 	.num_reg_defaults = ARRAY_SIZE(cs35l41_reg),
31 	.volatile_reg = cs35l41_volatile_reg,
32 	.readable_reg = cs35l41_readable_reg,
33 	.precious_reg = cs35l41_precious_reg,
34 	.cache_type = REGCACHE_RBTREE,
35 };
36 
37 static const struct spi_device_id cs35l41_id_spi[] = {
38 	{ "cs35l40", 0 },
39 	{ "cs35l41", 0 },
40 	{}
41 };
42 
43 MODULE_DEVICE_TABLE(spi, cs35l41_id_spi);
44 
45 static void cs35l41_spi_otp_setup(struct cs35l41_private *cs35l41,
46 				  bool is_pre_setup, unsigned int *freq)
47 {
48 	struct spi_device *spi;
49 	u32 orig_spi_freq;
50 
51 	spi = to_spi_device(cs35l41->dev);
52 
53 	if (!spi) {
54 		dev_err(cs35l41->dev, "%s: No SPI device\n", __func__);
55 		return;
56 	}
57 
58 	if (is_pre_setup) {
59 		orig_spi_freq = spi->max_speed_hz;
60 		if (orig_spi_freq > CS35L41_SPI_MAX_FREQ_OTP) {
61 			spi->max_speed_hz = CS35L41_SPI_MAX_FREQ_OTP;
62 			spi_setup(spi);
63 		}
64 		*freq = orig_spi_freq;
65 	} else {
66 		if (spi->max_speed_hz != *freq) {
67 			spi->max_speed_hz = *freq;
68 			spi_setup(spi);
69 		}
70 	}
71 }
72 
73 static int cs35l41_spi_probe(struct spi_device *spi)
74 {
75 	const struct regmap_config *regmap_config = &cs35l41_regmap_spi;
76 	struct cs35l41_platform_data *pdata = dev_get_platdata(&spi->dev);
77 	struct cs35l41_private *cs35l41;
78 	int ret;
79 
80 	cs35l41 = devm_kzalloc(&spi->dev, sizeof(struct cs35l41_private), GFP_KERNEL);
81 	if (!cs35l41)
82 		return -ENOMEM;
83 
84 	spi_set_drvdata(spi, cs35l41);
85 	cs35l41->regmap = devm_regmap_init_spi(spi, regmap_config);
86 	if (IS_ERR(cs35l41->regmap)) {
87 		ret = PTR_ERR(cs35l41->regmap);
88 		dev_err(&spi->dev, "Failed to allocate register map: %d\n", ret);
89 		return ret;
90 	}
91 
92 	cs35l41->dev = &spi->dev;
93 	cs35l41->irq = spi->irq;
94 	cs35l41->otp_setup = cs35l41_spi_otp_setup;
95 
96 	return cs35l41_probe(cs35l41, pdata);
97 }
98 
99 static int cs35l41_spi_remove(struct spi_device *spi)
100 {
101 	struct cs35l41_private *cs35l41 = spi_get_drvdata(spi);
102 
103 	cs35l41_remove(cs35l41);
104 
105 	return 0;
106 }
107 
108 #ifdef CONFIG_OF
109 static const struct of_device_id cs35l41_of_match[] = {
110 	{ .compatible = "cirrus,cs35l40" },
111 	{ .compatible = "cirrus,cs35l41" },
112 	{},
113 };
114 MODULE_DEVICE_TABLE(of, cs35l41_of_match);
115 #endif
116 
117 #ifdef CONFIG_ACPI
118 static const struct acpi_device_id cs35l41_acpi_match[] = {
119 	{ "CSC3541", 0 }, /* Cirrus Logic PnP ID + part ID */
120 	{},
121 };
122 MODULE_DEVICE_TABLE(acpi, cs35l41_acpi_match);
123 #endif
124 
125 static struct spi_driver cs35l41_spi_driver = {
126 	.driver = {
127 		.name		= "cs35l41",
128 		.of_match_table = of_match_ptr(cs35l41_of_match),
129 		.acpi_match_table = ACPI_PTR(cs35l41_acpi_match),
130 	},
131 	.id_table	= cs35l41_id_spi,
132 	.probe		= cs35l41_spi_probe,
133 	.remove		= cs35l41_spi_remove,
134 };
135 
136 module_spi_driver(cs35l41_spi_driver);
137 
138 MODULE_DESCRIPTION("SPI CS35L41 driver");
139 MODULE_AUTHOR("David Rhodes, Cirrus Logic Inc, <david.rhodes@cirrus.com>");
140 MODULE_LICENSE("GPL");
141