cs35l43-spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cs35l43-spi.c -- CS35l41 SPI driver
  4. *
  5. * Copyright 2017 Cirrus Logic, Inc.
  6. *
  7. * Author: David Rhodes <[email protected]>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/version.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/gpio.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/regulator/consumer.h>
  25. #include "wm_adsp.h"
  26. #include "cs35l43.h"
  27. #include <sound/cs35l43.h>
  28. static struct regmap_config cs35l43_regmap_spi = {
  29. .reg_bits = 32,
  30. .val_bits = 32,
  31. .pad_bits = 16,
  32. .reg_stride = 4,
  33. .reg_format_endian = REGMAP_ENDIAN_BIG,
  34. .val_format_endian = REGMAP_ENDIAN_BIG,
  35. .max_register = CS35L43_DSP1_PMEM_5114,
  36. .reg_defaults = cs35l43_reg,
  37. .num_reg_defaults = CS35L43_NUM_DEFAULTS,
  38. .volatile_reg = cs35l43_volatile_reg,
  39. .readable_reg = cs35l43_readable_reg,
  40. .precious_reg = cs35l43_precious_reg,
  41. .cache_type = REGCACHE_RBTREE,
  42. };
  43. static const struct spi_device_id cs35l43_id_spi[] = {
  44. {"cs35l43", 0},
  45. {}
  46. };
  47. MODULE_DEVICE_TABLE(spi, cs35l43_id_spi);
  48. static void cs35l43_limit_spi_clock(struct cs35l43_private *cs35l43, bool state)
  49. {
  50. struct spi_device *spi;
  51. spi = to_spi_device(cs35l43->dev);
  52. if (!spi) {
  53. dev_err(cs35l43->dev, "%s: No SPI device\n", __func__);
  54. return;
  55. }
  56. if (state)
  57. spi->max_speed_hz = CS35L43_SPI_MAX_FREQ_NO_PLL;
  58. else
  59. spi->max_speed_hz = cs35l43->max_spi_freq;
  60. dev_dbg(&spi->dev, "Set SPI freq: %d\n", spi->max_speed_hz);
  61. spi_setup(spi);
  62. }
  63. static int cs35l43_spi_probe(struct spi_device *spi)
  64. {
  65. const struct regmap_config *regmap_config = &cs35l43_regmap_spi;
  66. struct cs35l43_platform_data *pdata =
  67. dev_get_platdata(&spi->dev);
  68. struct cs35l43_private *cs35l43;
  69. int ret;
  70. cs35l43 = devm_kzalloc(&spi->dev,
  71. sizeof(struct cs35l43_private),
  72. GFP_KERNEL);
  73. if (cs35l43 == NULL)
  74. return -ENOMEM;
  75. spi_set_drvdata(spi, cs35l43);
  76. cs35l43->regmap = devm_regmap_init_spi(spi, regmap_config);
  77. if (IS_ERR(cs35l43->regmap)) {
  78. ret = PTR_ERR(cs35l43->regmap);
  79. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  80. ret);
  81. return ret;
  82. }
  83. cs35l43->dev = &spi->dev;
  84. cs35l43->irq = spi->irq;
  85. cs35l43->max_spi_freq = spi->max_speed_hz;
  86. cs35l43->limit_spi_clock = cs35l43_limit_spi_clock;
  87. cs35l43_limit_spi_clock(cs35l43, true);
  88. return cs35l43_probe(cs35l43, pdata);
  89. }
  90. static void cs35l43_spi_remove(struct spi_device *spi)
  91. {
  92. struct cs35l43_private *cs35l43 = spi_get_drvdata(spi);
  93. cs35l43_remove(cs35l43);
  94. }
  95. static const struct of_device_id cs35l43_of_match[] = {
  96. {.compatible = "cirrus,cs35l43"},
  97. {},
  98. };
  99. MODULE_DEVICE_TABLE(of, cs35l43_of_match);
  100. static struct spi_driver cs35l43_spi_driver = {
  101. .driver = {
  102. .name = "cs35l43",
  103. .of_match_table = cs35l43_of_match,
  104. .pm = &cs35l43_pm_ops,
  105. },
  106. .id_table = cs35l43_id_spi,
  107. .probe = cs35l43_spi_probe,
  108. .remove = cs35l43_spi_remove,
  109. };
  110. module_spi_driver(cs35l43_spi_driver);
  111. MODULE_DESCRIPTION("SPI CS35L43 driver");
  112. MODULE_AUTHOR("David Rhodes, Cirrus Logic Inc, <[email protected]>");
  113. MODULE_LICENSE("GPL");