1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SPI bus interface to Cirrus Logic Madera codecs 4 * 5 * Copyright (C) 2015-2018 Cirrus Logic 6 */ 7 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/regmap.h> 13 #include <linux/spi/spi.h> 14 15 #include <linux/mfd/madera/core.h> 16 17 #include "madera.h" 18 19 static int madera_spi_probe(struct spi_device *spi) 20 { 21 struct madera *madera; 22 const struct regmap_config *regmap_16bit_config = NULL; 23 const struct regmap_config *regmap_32bit_config = NULL; 24 unsigned long type; 25 const char *name; 26 int ret; 27 28 type = (unsigned long)spi_get_device_match_data(spi); 29 switch (type) { 30 case CS47L15: 31 if (IS_ENABLED(CONFIG_MFD_CS47L15)) { 32 regmap_16bit_config = &cs47l15_16bit_spi_regmap; 33 regmap_32bit_config = &cs47l15_32bit_spi_regmap; 34 } 35 break; 36 case CS47L35: 37 if (IS_ENABLED(CONFIG_MFD_CS47L35)) { 38 regmap_16bit_config = &cs47l35_16bit_spi_regmap; 39 regmap_32bit_config = &cs47l35_32bit_spi_regmap; 40 } 41 break; 42 case CS47L85: 43 case WM1840: 44 if (IS_ENABLED(CONFIG_MFD_CS47L85)) { 45 regmap_16bit_config = &cs47l85_16bit_spi_regmap; 46 regmap_32bit_config = &cs47l85_32bit_spi_regmap; 47 } 48 break; 49 case CS47L90: 50 case CS47L91: 51 if (IS_ENABLED(CONFIG_MFD_CS47L90)) { 52 regmap_16bit_config = &cs47l90_16bit_spi_regmap; 53 regmap_32bit_config = &cs47l90_32bit_spi_regmap; 54 } 55 break; 56 case CS42L92: 57 case CS47L92: 58 case CS47L93: 59 if (IS_ENABLED(CONFIG_MFD_CS47L92)) { 60 regmap_16bit_config = &cs47l92_16bit_spi_regmap; 61 regmap_32bit_config = &cs47l92_32bit_spi_regmap; 62 } 63 break; 64 default: 65 dev_err(&spi->dev, 66 "Unknown Madera SPI device type %ld\n", type); 67 return -EINVAL; 68 } 69 70 name = madera_name_from_type(type); 71 72 if (!regmap_16bit_config) { 73 /* it's polite to say which codec isn't built into the kernel */ 74 dev_err(&spi->dev, 75 "Kernel does not include support for %s\n", name); 76 return -EINVAL; 77 } 78 79 madera = devm_kzalloc(&spi->dev, sizeof(*madera), GFP_KERNEL); 80 if (!madera) 81 return -ENOMEM; 82 83 madera->regmap = devm_regmap_init_spi(spi, regmap_16bit_config); 84 if (IS_ERR(madera->regmap)) { 85 ret = PTR_ERR(madera->regmap); 86 dev_err(&spi->dev, 87 "Failed to allocate 16-bit register map: %d\n", ret); 88 return ret; 89 } 90 91 madera->regmap_32bit = devm_regmap_init_spi(spi, regmap_32bit_config); 92 if (IS_ERR(madera->regmap_32bit)) { 93 ret = PTR_ERR(madera->regmap_32bit); 94 dev_err(&spi->dev, 95 "Failed to allocate 32-bit register map: %d\n", ret); 96 return ret; 97 } 98 99 madera->type = type; 100 madera->type_name = name; 101 madera->dev = &spi->dev; 102 madera->irq = spi->irq; 103 104 return madera_dev_init(madera); 105 } 106 107 static void madera_spi_remove(struct spi_device *spi) 108 { 109 struct madera *madera = spi_get_drvdata(spi); 110 111 madera_dev_exit(madera); 112 } 113 114 static const struct spi_device_id madera_spi_ids[] = { 115 { "cs47l15", CS47L15 }, 116 { "cs47l35", CS47L35 }, 117 { "cs47l85", CS47L85 }, 118 { "cs47l90", CS47L90 }, 119 { "cs47l91", CS47L91 }, 120 { "cs42l92", CS42L92 }, 121 { "cs47l92", CS47L92 }, 122 { "cs47l93", CS47L93 }, 123 { "wm1840", WM1840 }, 124 { } 125 }; 126 MODULE_DEVICE_TABLE(spi, madera_spi_ids); 127 128 static struct spi_driver madera_spi_driver = { 129 .driver = { 130 .name = "madera", 131 .pm = &madera_pm_ops, 132 .of_match_table = of_match_ptr(madera_of_match), 133 }, 134 .probe = madera_spi_probe, 135 .remove = madera_spi_remove, 136 .id_table = madera_spi_ids, 137 }; 138 139 module_spi_driver(madera_spi_driver); 140 141 MODULE_DESCRIPTION("Madera SPI bus interface"); 142 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 143 MODULE_LICENSE("GPL v2"); 144