mc13xxx-spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2009-2010 Pengutronix
  4. * Uwe Kleine-Koenig <[email protected]>
  5. *
  6. * loosely based on an earlier driver that has
  7. * Copyright 2009 Pengutronix, Sascha Hauer <[email protected]>
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mfd/core.h>
  14. #include <linux/mfd/mc13xxx.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/err.h>
  19. #include <linux/spi/spi.h>
  20. #include "mc13xxx.h"
  21. static const struct spi_device_id mc13xxx_device_id[] = {
  22. {
  23. .name = "mc13783",
  24. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783,
  25. }, {
  26. .name = "mc13892",
  27. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
  28. }, {
  29. .name = "mc34708",
  30. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
  31. }, {
  32. /* sentinel */
  33. }
  34. };
  35. MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
  36. static const struct of_device_id mc13xxx_dt_ids[] = {
  37. { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, },
  38. { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, },
  39. { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, },
  40. { /* sentinel */ }
  41. };
  42. MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
  43. static const struct regmap_config mc13xxx_regmap_spi_config = {
  44. .reg_bits = 7,
  45. .pad_bits = 1,
  46. .val_bits = 24,
  47. .write_flag_mask = 0x80,
  48. .max_register = MC13XXX_NUMREGS,
  49. .cache_type = REGCACHE_NONE,
  50. .use_single_read = true,
  51. .use_single_write = true,
  52. };
  53. static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size,
  54. void *val, size_t val_size)
  55. {
  56. unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0};
  57. unsigned char r[4];
  58. unsigned char *p = val;
  59. struct device *dev = context;
  60. struct spi_device *spi = to_spi_device(dev);
  61. struct spi_transfer t = {
  62. .tx_buf = w,
  63. .rx_buf = r,
  64. .len = 4,
  65. };
  66. struct spi_message m;
  67. int ret;
  68. if (val_size != 3 || reg_size != 1)
  69. return -ENOTSUPP;
  70. spi_message_init(&m);
  71. spi_message_add_tail(&t, &m);
  72. ret = spi_sync(spi, &m);
  73. memcpy(p, &r[1], 3);
  74. return ret;
  75. }
  76. static int mc13xxx_spi_write(void *context, const void *data, size_t count)
  77. {
  78. struct device *dev = context;
  79. struct spi_device *spi = to_spi_device(dev);
  80. const char *reg = data;
  81. if (count != 4)
  82. return -ENOTSUPP;
  83. /* include errata fix for spi audio problems */
  84. if (*reg == MC13783_AUDIO_CODEC || *reg == MC13783_AUDIO_DAC)
  85. spi_write(spi, data, count);
  86. return spi_write(spi, data, count);
  87. }
  88. /*
  89. * We cannot use regmap-spi generic bus implementation here.
  90. * The MC13783 chip will get corrupted if CS signal is deasserted
  91. * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
  92. * has the following errata (DSPhl22960):
  93. * "The CSPI negates SS when the FIFO becomes empty with
  94. * SSCTL= 0. Software cannot guarantee that the FIFO will not
  95. * drain because of higher priority interrupts and the
  96. * non-realtime characteristics of the operating system. As a
  97. * result, the SS will negate before all of the data has been
  98. * transferred to/from the peripheral."
  99. * We workaround this by accessing the SPI controller with a
  100. * single transfert.
  101. */
  102. static struct regmap_bus regmap_mc13xxx_bus = {
  103. .write = mc13xxx_spi_write,
  104. .read = mc13xxx_spi_read,
  105. };
  106. static int mc13xxx_spi_probe(struct spi_device *spi)
  107. {
  108. struct mc13xxx *mc13xxx;
  109. int ret;
  110. mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL);
  111. if (!mc13xxx)
  112. return -ENOMEM;
  113. dev_set_drvdata(&spi->dev, mc13xxx);
  114. spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
  115. mc13xxx->irq = spi->irq;
  116. spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
  117. ret = spi_setup(spi);
  118. if (ret)
  119. return ret;
  120. mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
  121. &spi->dev,
  122. &mc13xxx_regmap_spi_config);
  123. if (IS_ERR(mc13xxx->regmap)) {
  124. ret = PTR_ERR(mc13xxx->regmap);
  125. dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret);
  126. return ret;
  127. }
  128. if (spi->dev.of_node) {
  129. const struct of_device_id *of_id =
  130. of_match_device(mc13xxx_dt_ids, &spi->dev);
  131. mc13xxx->variant = of_id->data;
  132. } else {
  133. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  134. mc13xxx->variant = (void *)id_entry->driver_data;
  135. }
  136. return mc13xxx_common_init(&spi->dev);
  137. }
  138. static void mc13xxx_spi_remove(struct spi_device *spi)
  139. {
  140. mc13xxx_common_exit(&spi->dev);
  141. }
  142. static struct spi_driver mc13xxx_spi_driver = {
  143. .id_table = mc13xxx_device_id,
  144. .driver = {
  145. .name = "mc13xxx",
  146. .of_match_table = mc13xxx_dt_ids,
  147. },
  148. .probe = mc13xxx_spi_probe,
  149. .remove = mc13xxx_spi_remove,
  150. };
  151. static int __init mc13xxx_init(void)
  152. {
  153. return spi_register_driver(&mc13xxx_spi_driver);
  154. }
  155. subsys_initcall(mc13xxx_init);
  156. static void __exit mc13xxx_exit(void)
  157. {
  158. spi_unregister_driver(&mc13xxx_spi_driver);
  159. }
  160. module_exit(mc13xxx_exit);
  161. MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
  162. MODULE_AUTHOR("Uwe Kleine-Koenig <[email protected]>");
  163. MODULE_LICENSE("GPL v2");