madera-spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SPI 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. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/mfd/madera/core.h>
  19. #include "madera.h"
  20. static int madera_spi_probe(struct spi_device *spi)
  21. {
  22. const struct spi_device_id *id = spi_get_device_id(spi);
  23. struct madera *madera;
  24. const struct regmap_config *regmap_16bit_config = NULL;
  25. const struct regmap_config *regmap_32bit_config = NULL;
  26. const void *of_data;
  27. unsigned long type;
  28. const char *name;
  29. int ret;
  30. of_data = of_device_get_match_data(&spi->dev);
  31. if (of_data)
  32. type = (unsigned long)of_data;
  33. else
  34. type = id->driver_data;
  35. switch (type) {
  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. default:
  57. dev_err(&spi->dev,
  58. "Unknown Madera SPI device type %ld\n", type);
  59. return -EINVAL;
  60. }
  61. name = madera_name_from_type(type);
  62. if (!regmap_16bit_config) {
  63. /* it's polite to say which codec isn't built into the kernel */
  64. dev_err(&spi->dev,
  65. "Kernel does not include support for %s\n", name);
  66. return -EINVAL;
  67. }
  68. madera = devm_kzalloc(&spi->dev, sizeof(*madera), GFP_KERNEL);
  69. if (!madera)
  70. return -ENOMEM;
  71. madera->regmap = devm_regmap_init_spi(spi, regmap_16bit_config);
  72. if (IS_ERR(madera->regmap)) {
  73. ret = PTR_ERR(madera->regmap);
  74. dev_err(&spi->dev,
  75. "Failed to allocate 16-bit register map: %d\n", ret);
  76. return ret;
  77. }
  78. madera->regmap_32bit = devm_regmap_init_spi(spi, regmap_32bit_config);
  79. if (IS_ERR(madera->regmap_32bit)) {
  80. ret = PTR_ERR(madera->regmap_32bit);
  81. dev_err(&spi->dev,
  82. "Failed to allocate 32-bit register map: %d\n", ret);
  83. return ret;
  84. }
  85. madera->type = type;
  86. madera->type_name = name;
  87. madera->dev = &spi->dev;
  88. madera->irq = spi->irq;
  89. return madera_dev_init(madera);
  90. }
  91. static int madera_spi_remove(struct spi_device *spi)
  92. {
  93. struct madera *madera = spi_get_drvdata(spi);
  94. madera_dev_exit(madera);
  95. return 0;
  96. }
  97. static const struct spi_device_id madera_spi_ids[] = {
  98. { "cs47l35", CS47L35 },
  99. { "cs47l85", CS47L85 },
  100. { "cs47l90", CS47L90 },
  101. { "cs47l91", CS47L91 },
  102. { "wm1840", WM1840 },
  103. { }
  104. };
  105. MODULE_DEVICE_TABLE(spi, madera_spi_ids);
  106. static struct spi_driver madera_spi_driver = {
  107. .driver = {
  108. .name = "madera",
  109. .pm = &madera_pm_ops,
  110. .of_match_table = of_match_ptr(madera_of_match),
  111. },
  112. .probe = madera_spi_probe,
  113. .remove = madera_spi_remove,
  114. .id_table = madera_spi_ids,
  115. };
  116. module_spi_driver(madera_spi_driver);
  117. MODULE_DESCRIPTION("Madera SPI bus interface");
  118. MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
  119. MODULE_LICENSE("GPL v2");