1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * I2C bus interface to Cirrus Logic Madera codecs 4 * 5 * Copyright (C) 2015-2018 Cirrus Logic 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; version 2. 10 */ 11 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/i2c.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/of_device.h> 18 #include <linux/regmap.h> 19 20 #include <linux/mfd/madera/core.h> 21 22 #include "madera.h" 23 24 static int madera_i2c_probe(struct i2c_client *i2c, 25 const struct i2c_device_id *id) 26 { 27 struct madera *madera; 28 const struct regmap_config *regmap_16bit_config = NULL; 29 const struct regmap_config *regmap_32bit_config = NULL; 30 const void *of_data; 31 unsigned long type; 32 const char *name; 33 int ret; 34 35 of_data = of_device_get_match_data(&i2c->dev); 36 if (of_data) 37 type = (unsigned long)of_data; 38 else 39 type = id->driver_data; 40 41 switch (type) { 42 case CS47L15: 43 if (IS_ENABLED(CONFIG_MFD_CS47L15)) { 44 regmap_16bit_config = &cs47l15_16bit_i2c_regmap; 45 regmap_32bit_config = &cs47l15_32bit_i2c_regmap; 46 } 47 break; 48 case CS47L35: 49 if (IS_ENABLED(CONFIG_MFD_CS47L35)) { 50 regmap_16bit_config = &cs47l35_16bit_i2c_regmap; 51 regmap_32bit_config = &cs47l35_32bit_i2c_regmap; 52 } 53 break; 54 case CS47L85: 55 case WM1840: 56 if (IS_ENABLED(CONFIG_MFD_CS47L85)) { 57 regmap_16bit_config = &cs47l85_16bit_i2c_regmap; 58 regmap_32bit_config = &cs47l85_32bit_i2c_regmap; 59 } 60 break; 61 case CS47L90: 62 case CS47L91: 63 if (IS_ENABLED(CONFIG_MFD_CS47L90)) { 64 regmap_16bit_config = &cs47l90_16bit_i2c_regmap; 65 regmap_32bit_config = &cs47l90_32bit_i2c_regmap; 66 } 67 break; 68 case CS42L92: 69 case CS47L92: 70 case CS47L93: 71 if (IS_ENABLED(CONFIG_MFD_CS47L92)) { 72 regmap_16bit_config = &cs47l92_16bit_i2c_regmap; 73 regmap_32bit_config = &cs47l92_32bit_i2c_regmap; 74 } 75 break; 76 default: 77 dev_err(&i2c->dev, 78 "Unknown Madera I2C device type %ld\n", type); 79 return -EINVAL; 80 } 81 82 name = madera_name_from_type(type); 83 84 if (!regmap_16bit_config) { 85 /* it's polite to say which codec isn't built into the kernel */ 86 dev_err(&i2c->dev, 87 "Kernel does not include support for %s\n", name); 88 return -EINVAL; 89 } 90 91 madera = devm_kzalloc(&i2c->dev, sizeof(*madera), GFP_KERNEL); 92 if (!madera) 93 return -ENOMEM; 94 95 96 madera->regmap = devm_regmap_init_i2c(i2c, regmap_16bit_config); 97 if (IS_ERR(madera->regmap)) { 98 ret = PTR_ERR(madera->regmap); 99 dev_err(&i2c->dev, 100 "Failed to allocate 16-bit register map: %d\n", ret); 101 return ret; 102 } 103 104 madera->regmap_32bit = devm_regmap_init_i2c(i2c, regmap_32bit_config); 105 if (IS_ERR(madera->regmap_32bit)) { 106 ret = PTR_ERR(madera->regmap_32bit); 107 dev_err(&i2c->dev, 108 "Failed to allocate 32-bit register map: %d\n", ret); 109 return ret; 110 } 111 112 madera->type = type; 113 madera->type_name = name; 114 madera->dev = &i2c->dev; 115 madera->irq = i2c->irq; 116 117 return madera_dev_init(madera); 118 } 119 120 static int madera_i2c_remove(struct i2c_client *i2c) 121 { 122 struct madera *madera = dev_get_drvdata(&i2c->dev); 123 124 madera_dev_exit(madera); 125 126 return 0; 127 } 128 129 static const struct i2c_device_id madera_i2c_id[] = { 130 { "cs47l15", CS47L15 }, 131 { "cs47l35", CS47L35 }, 132 { "cs47l85", CS47L85 }, 133 { "cs47l90", CS47L90 }, 134 { "cs47l91", CS47L91 }, 135 { "cs42l92", CS42L92 }, 136 { "cs47l92", CS47L92 }, 137 { "cs47l93", CS47L93 }, 138 { "wm1840", WM1840 }, 139 { } 140 }; 141 MODULE_DEVICE_TABLE(i2c, madera_i2c_id); 142 143 static struct i2c_driver madera_i2c_driver = { 144 .driver = { 145 .name = "madera", 146 .pm = &madera_pm_ops, 147 .of_match_table = of_match_ptr(madera_of_match), 148 }, 149 .probe = madera_i2c_probe, 150 .remove = madera_i2c_remove, 151 .id_table = madera_i2c_id, 152 }; 153 154 module_i2c_driver(madera_i2c_driver); 155 156 MODULE_DESCRIPTION("Madera I2C bus interface"); 157 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 158 MODULE_LICENSE("GPL v2"); 159