Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * I2C bus interface to Cirrus Logic Madera codecs
0004  *
0005  * Copyright (C) 2015-2018 Cirrus Logic
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/err.h>
0010 #include <linux/i2c.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/of_device.h>
0014 #include <linux/regmap.h>
0015 
0016 #include <linux/mfd/madera/core.h>
0017 
0018 #include "madera.h"
0019 
0020 static int madera_i2c_probe(struct i2c_client *i2c,
0021                 const struct i2c_device_id *id)
0022 {
0023     struct madera *madera;
0024     const struct regmap_config *regmap_16bit_config = NULL;
0025     const struct regmap_config *regmap_32bit_config = NULL;
0026     const void *of_data;
0027     unsigned long type;
0028     const char *name;
0029     int ret;
0030 
0031     of_data = of_device_get_match_data(&i2c->dev);
0032     if (of_data)
0033         type = (unsigned long)of_data;
0034     else
0035         type = id->driver_data;
0036 
0037     switch (type) {
0038     case CS47L15:
0039         if (IS_ENABLED(CONFIG_MFD_CS47L15)) {
0040             regmap_16bit_config = &cs47l15_16bit_i2c_regmap;
0041             regmap_32bit_config = &cs47l15_32bit_i2c_regmap;
0042         }
0043         break;
0044     case CS47L35:
0045         if (IS_ENABLED(CONFIG_MFD_CS47L35)) {
0046             regmap_16bit_config = &cs47l35_16bit_i2c_regmap;
0047             regmap_32bit_config = &cs47l35_32bit_i2c_regmap;
0048         }
0049         break;
0050     case CS47L85:
0051     case WM1840:
0052         if (IS_ENABLED(CONFIG_MFD_CS47L85)) {
0053             regmap_16bit_config = &cs47l85_16bit_i2c_regmap;
0054             regmap_32bit_config = &cs47l85_32bit_i2c_regmap;
0055         }
0056         break;
0057     case CS47L90:
0058     case CS47L91:
0059         if (IS_ENABLED(CONFIG_MFD_CS47L90)) {
0060             regmap_16bit_config = &cs47l90_16bit_i2c_regmap;
0061             regmap_32bit_config = &cs47l90_32bit_i2c_regmap;
0062         }
0063         break;
0064     case CS42L92:
0065     case CS47L92:
0066     case CS47L93:
0067         if (IS_ENABLED(CONFIG_MFD_CS47L92)) {
0068             regmap_16bit_config = &cs47l92_16bit_i2c_regmap;
0069             regmap_32bit_config = &cs47l92_32bit_i2c_regmap;
0070         }
0071         break;
0072     default:
0073         dev_err(&i2c->dev,
0074             "Unknown Madera I2C device type %ld\n", type);
0075         return -EINVAL;
0076     }
0077 
0078     name = madera_name_from_type(type);
0079 
0080     if (!regmap_16bit_config) {
0081         /* it's polite to say which codec isn't built into the kernel */
0082         dev_err(&i2c->dev,
0083             "Kernel does not include support for %s\n", name);
0084         return -EINVAL;
0085     }
0086 
0087     madera = devm_kzalloc(&i2c->dev, sizeof(*madera), GFP_KERNEL);
0088     if (!madera)
0089         return -ENOMEM;
0090 
0091     madera->regmap = devm_regmap_init_i2c(i2c, regmap_16bit_config);
0092     if (IS_ERR(madera->regmap)) {
0093         ret = PTR_ERR(madera->regmap);
0094         dev_err(&i2c->dev,
0095             "Failed to allocate 16-bit register map: %d\n", ret);
0096         return ret;
0097     }
0098 
0099     madera->regmap_32bit = devm_regmap_init_i2c(i2c, regmap_32bit_config);
0100     if (IS_ERR(madera->regmap_32bit)) {
0101         ret = PTR_ERR(madera->regmap_32bit);
0102         dev_err(&i2c->dev,
0103             "Failed to allocate 32-bit register map: %d\n", ret);
0104         return ret;
0105     }
0106 
0107     madera->type = type;
0108     madera->type_name = name;
0109     madera->dev = &i2c->dev;
0110     madera->irq = i2c->irq;
0111 
0112     return madera_dev_init(madera);
0113 }
0114 
0115 static int madera_i2c_remove(struct i2c_client *i2c)
0116 {
0117     struct madera *madera = dev_get_drvdata(&i2c->dev);
0118 
0119     madera_dev_exit(madera);
0120 
0121     return 0;
0122 }
0123 
0124 static const struct i2c_device_id madera_i2c_id[] = {
0125     { "cs47l15", CS47L15 },
0126     { "cs47l35", CS47L35 },
0127     { "cs47l85", CS47L85 },
0128     { "cs47l90", CS47L90 },
0129     { "cs47l91", CS47L91 },
0130     { "cs42l92", CS42L92 },
0131     { "cs47l92", CS47L92 },
0132     { "cs47l93", CS47L93 },
0133     { "wm1840", WM1840 },
0134     { }
0135 };
0136 MODULE_DEVICE_TABLE(i2c, madera_i2c_id);
0137 
0138 static struct i2c_driver madera_i2c_driver = {
0139     .driver = {
0140         .name   = "madera",
0141         .pm = &madera_pm_ops,
0142         .of_match_table = of_match_ptr(madera_of_match),
0143     },
0144     .probe      = madera_i2c_probe,
0145     .remove     = madera_i2c_remove,
0146     .id_table   = madera_i2c_id,
0147 };
0148 
0149 module_i2c_driver(madera_i2c_driver);
0150 
0151 MODULE_DESCRIPTION("Madera I2C bus interface");
0152 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
0153 MODULE_LICENSE("GPL v2");