xref: /openbmc/linux/sound/soc/codecs/cs42l42-i2c.c (revision 94d5f62a91aab6ac9c3f4abfd048cbe5f77153ac)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * cs42l42-i2c.c -- CS42L42 ALSA SoC audio driver for I2C
4  *
5  * Copyright 2016, 2022 Cirrus Logic, Inc.
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 
14 #include "cs42l42.h"
15 
16 static int cs42l42_i2c_probe(struct i2c_client *i2c_client)
17 {
18 	struct device *dev = &i2c_client->dev;
19 	struct cs42l42_private *cs42l42;
20 	struct regmap *regmap;
21 	int ret;
22 
23 	cs42l42 = devm_kzalloc(dev, sizeof(*cs42l42), GFP_KERNEL);
24 	if (!cs42l42)
25 		return -ENOMEM;
26 
27 	regmap = devm_regmap_init_i2c(i2c_client, &cs42l42_regmap);
28 	if (IS_ERR(regmap)) {
29 		ret = PTR_ERR(regmap);
30 		dev_err(&i2c_client->dev, "regmap_init() failed: %d\n", ret);
31 		return ret;
32 	}
33 
34 	cs42l42->devid = CS42L42_CHIP_ID;
35 	cs42l42->dev = dev;
36 	cs42l42->regmap = regmap;
37 	cs42l42->irq = i2c_client->irq;
38 
39 	ret = cs42l42_common_probe(cs42l42, &cs42l42_soc_component, &cs42l42_dai);
40 	if (ret)
41 		return ret;
42 
43 	return cs42l42_init(cs42l42);
44 }
45 
46 static int cs42l42_i2c_remove(struct i2c_client *i2c_client)
47 {
48 	struct cs42l42_private *cs42l42 = dev_get_drvdata(&i2c_client->dev);
49 
50 	cs42l42_common_remove(cs42l42);
51 
52 	return 0;
53 }
54 
55 static int __maybe_unused cs42l42_i2c_resume(struct device *dev)
56 {
57 	int ret;
58 
59 	ret = cs42l42_resume(dev);
60 	if (ret)
61 		return ret;
62 
63 	cs42l42_resume_restore(dev);
64 
65 	return 0;
66 }
67 
68 static const struct dev_pm_ops cs42l42_i2c_pm_ops = {
69 	SET_SYSTEM_SLEEP_PM_OPS(cs42l42_suspend, cs42l42_i2c_resume)
70 };
71 
72 static const struct of_device_id __maybe_unused cs42l42_of_match[] = {
73 	{ .compatible = "cirrus,cs42l42", },
74 	{}
75 };
76 MODULE_DEVICE_TABLE(of, cs42l42_of_match);
77 
78 static const struct acpi_device_id __maybe_unused cs42l42_acpi_match[] = {
79 	{"10134242", 0,},
80 	{}
81 };
82 MODULE_DEVICE_TABLE(acpi, cs42l42_acpi_match);
83 
84 static const struct i2c_device_id cs42l42_id[] = {
85 	{"cs42l42", 0},
86 	{}
87 };
88 
89 MODULE_DEVICE_TABLE(i2c, cs42l42_id);
90 
91 static struct i2c_driver cs42l42_i2c_driver = {
92 	.driver = {
93 		.name = "cs42l42",
94 		.pm = &cs42l42_i2c_pm_ops,
95 		.of_match_table = of_match_ptr(cs42l42_of_match),
96 		.acpi_match_table = ACPI_PTR(cs42l42_acpi_match),
97 		},
98 	.id_table = cs42l42_id,
99 	.probe_new = cs42l42_i2c_probe,
100 	.remove = cs42l42_i2c_remove,
101 };
102 
103 module_i2c_driver(cs42l42_i2c_driver);
104 
105 MODULE_DESCRIPTION("ASoC CS42L42 I2C driver");
106 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
107 MODULE_LICENSE("GPL");
108 MODULE_IMPORT_NS(SND_SOC_CS42L42_CORE);
109