mc13xxx-spi.c 5.0 KB

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