GNU Linux-libre 6.8.7-gnu
[releases.git] / drivers / mfd / cs42l43-i2c.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CS42L43 I2C driver
4  *
5  * Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6  *                         Cirrus Logic International Semiconductor Ltd.
7  */
8
9 #include <linux/err.h>
10 #include <linux/errno.h>
11 #include <linux/i2c.h>
12 #include <linux/mfd/cs42l43-regs.h>
13 #include <linux/module.h>
14
15 #include "cs42l43.h"
16
17 static const struct regmap_config cs42l43_i2c_regmap = {
18         .reg_bits               = 32,
19         .reg_stride             = 4,
20         .val_bits               = 32,
21         .reg_format_endian      = REGMAP_ENDIAN_BIG,
22         .val_format_endian      = REGMAP_ENDIAN_BIG,
23
24         .max_register           = CS42L43_MCU_RAM_MAX,
25         .readable_reg           = cs42l43_readable_register,
26         .volatile_reg           = cs42l43_volatile_register,
27         .precious_reg           = cs42l43_precious_register,
28
29         .cache_type             = REGCACHE_MAPLE,
30         .reg_defaults           = cs42l43_reg_default,
31         .num_reg_defaults       = ARRAY_SIZE(cs42l43_reg_default),
32 };
33
34 static int cs42l43_i2c_probe(struct i2c_client *i2c)
35 {
36         struct cs42l43 *cs42l43;
37         int ret;
38
39         cs42l43 = devm_kzalloc(&i2c->dev, sizeof(*cs42l43), GFP_KERNEL);
40         if (!cs42l43)
41                 return -ENOMEM;
42
43         cs42l43->dev = &i2c->dev;
44         cs42l43->irq = i2c->irq;
45         /* A device on an I2C is always attached by definition. */
46         cs42l43->attached = true;
47
48         cs42l43->regmap = devm_regmap_init_i2c(i2c, &cs42l43_i2c_regmap);
49         if (IS_ERR(cs42l43->regmap)) {
50                 ret = PTR_ERR(cs42l43->regmap);
51                 dev_err(cs42l43->dev, "Failed to allocate regmap: %d\n", ret);
52                 return ret;
53         }
54
55         return cs42l43_dev_probe(cs42l43);
56 }
57
58 static void cs42l43_i2c_remove(struct i2c_client *i2c)
59 {
60         struct cs42l43 *cs42l43 = dev_get_drvdata(&i2c->dev);
61
62         cs42l43_dev_remove(cs42l43);
63 }
64
65 #if IS_ENABLED(CONFIG_OF)
66 static const struct of_device_id cs42l43_of_match[] = {
67         { .compatible = "cirrus,cs42l43", },
68         {}
69 };
70 MODULE_DEVICE_TABLE(of, cs42l43_of_match);
71 #endif
72
73 #if IS_ENABLED(CONFIG_ACPI)
74 static const struct acpi_device_id cs42l43_acpi_match[] = {
75         { "CSC4243", 0 },
76         {}
77 };
78 MODULE_DEVICE_TABLE(acpi, cs42l43_acpi_match);
79 #endif
80
81 static struct i2c_driver cs42l43_i2c_driver = {
82         .driver = {
83                 .name                   = "cs42l43",
84                 .pm                     = pm_ptr(&cs42l43_pm_ops),
85                 .of_match_table         = of_match_ptr(cs42l43_of_match),
86                 .acpi_match_table       = ACPI_PTR(cs42l43_acpi_match),
87         },
88
89         .probe          = cs42l43_i2c_probe,
90         .remove         = cs42l43_i2c_remove,
91 };
92 module_i2c_driver(cs42l43_i2c_driver);
93
94 MODULE_IMPORT_NS(MFD_CS42L43);
95
96 MODULE_DESCRIPTION("CS42L43 I2C Driver");
97 MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
98 MODULE_LICENSE("GPL");