xref: /openbmc/linux/sound/soc/codecs/cs35l41-i2c.c (revision 1f012283)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // cs35l41-i2c.c -- CS35l41 I2C 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/i2c.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 
20 #include <sound/cs35l41.h>
21 #include "cs35l41.h"
22 
23 static struct regmap_config cs35l41_regmap_i2c = {
24 	.reg_bits = 32,
25 	.val_bits = 32,
26 	.reg_stride = CS35L41_REGSTRIDE,
27 	.reg_format_endian = REGMAP_ENDIAN_BIG,
28 	.val_format_endian = REGMAP_ENDIAN_BIG,
29 	.max_register = CS35L41_LASTREG,
30 	.reg_defaults = cs35l41_reg,
31 	.num_reg_defaults = ARRAY_SIZE(cs35l41_reg),
32 	.volatile_reg = cs35l41_volatile_reg,
33 	.readable_reg = cs35l41_readable_reg,
34 	.precious_reg = cs35l41_precious_reg,
35 	.cache_type = REGCACHE_RBTREE,
36 };
37 
38 static const struct i2c_device_id cs35l41_id_i2c[] = {
39 	{ "cs35l40", 0 },
40 	{ "cs35l41", 0 },
41 	{}
42 };
43 
44 MODULE_DEVICE_TABLE(i2c, cs35l41_id_i2c);
45 
46 static int cs35l41_i2c_probe(struct i2c_client *client,
47 			     const struct i2c_device_id *id)
48 {
49 	struct cs35l41_private *cs35l41;
50 	struct device *dev = &client->dev;
51 	struct cs35l41_platform_data *pdata = dev_get_platdata(dev);
52 	const struct regmap_config *regmap_config = &cs35l41_regmap_i2c;
53 	int ret;
54 
55 	cs35l41 = devm_kzalloc(dev, sizeof(struct cs35l41_private), GFP_KERNEL);
56 
57 	if (!cs35l41)
58 		return -ENOMEM;
59 
60 	cs35l41->dev = dev;
61 	cs35l41->irq = client->irq;
62 
63 	i2c_set_clientdata(client, cs35l41);
64 	cs35l41->regmap = devm_regmap_init_i2c(client, regmap_config);
65 	if (IS_ERR(cs35l41->regmap)) {
66 		ret = PTR_ERR(cs35l41->regmap);
67 		dev_err(cs35l41->dev, "Failed to allocate register map: %d\n", ret);
68 		return ret;
69 	}
70 
71 	return cs35l41_probe(cs35l41, pdata);
72 }
73 
74 static int cs35l41_i2c_remove(struct i2c_client *client)
75 {
76 	struct cs35l41_private *cs35l41 = i2c_get_clientdata(client);
77 
78 	cs35l41_remove(cs35l41);
79 
80 	return 0;
81 }
82 
83 #ifdef CONFIG_OF
84 static const struct of_device_id cs35l41_of_match[] = {
85 	{ .compatible = "cirrus,cs35l40" },
86 	{ .compatible = "cirrus,cs35l41" },
87 	{},
88 };
89 MODULE_DEVICE_TABLE(of, cs35l41_of_match);
90 #endif
91 
92 #ifdef CONFIG_ACPI
93 static const struct acpi_device_id cs35l41_acpi_match[] = {
94 	{ "CSC3541", 0 }, /* Cirrus Logic PnP ID + part ID */
95 	{},
96 };
97 MODULE_DEVICE_TABLE(acpi, cs35l41_acpi_match);
98 #endif
99 
100 static struct i2c_driver cs35l41_i2c_driver = {
101 	.driver = {
102 		.name		= "cs35l41",
103 		.of_match_table = of_match_ptr(cs35l41_of_match),
104 		.acpi_match_table = ACPI_PTR(cs35l41_acpi_match),
105 	},
106 	.id_table	= cs35l41_id_i2c,
107 	.probe		= cs35l41_i2c_probe,
108 	.remove		= cs35l41_i2c_remove,
109 };
110 
111 module_i2c_driver(cs35l41_i2c_driver);
112 
113 MODULE_DESCRIPTION("I2C CS35L41 driver");
114 MODULE_AUTHOR("David Rhodes, Cirrus Logic Inc, <david.rhodes@cirrus.com>");
115 MODULE_LICENSE("GPL");
116