mc13xxx-spi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/mc13xxx.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/err.h>
  23. #include <linux/spi/spi.h>
  24. #include "mc13xxx.h"
  25. static const struct spi_device_id mc13xxx_device_id[] = {
  26. {
  27. .name = "mc13783",
  28. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783,
  29. }, {
  30. .name = "mc13892",
  31. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
  32. }, {
  33. .name = "mc34708",
  34. .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
  35. }, {
  36. /* sentinel */
  37. }
  38. };
  39. MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
  40. static const struct of_device_id mc13xxx_dt_ids[] = {
  41. { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, },
  42. { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, },
  43. { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, },
  44. { /* sentinel */ }
  45. };
  46. MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
  47. static struct regmap_config mc13xxx_regmap_spi_config = {
  48. .reg_bits = 7,
  49. .pad_bits = 1,
  50. .val_bits = 24,
  51. .write_flag_mask = 0x80,
  52. .max_register = MC13XXX_NUMREGS,
  53. .cache_type = REGCACHE_NONE,
  54. .use_single_rw = 1,
  55. };
  56. static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size,
  57. void *val, size_t val_size)
  58. {
  59. unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0};
  60. unsigned char r[4];
  61. unsigned char *p = val;
  62. struct device *dev = context;
  63. struct spi_device *spi = to_spi_device(dev);
  64. struct spi_transfer t = {
  65. .tx_buf = w,
  66. .rx_buf = r,
  67. .len = 4,
  68. };
  69. struct spi_message m;
  70. int ret;
  71. if (val_size != 3 || reg_size != 1)
  72. return -ENOTSUPP;
  73. spi_message_init(&m);
  74. spi_message_add_tail(&t, &m);
  75. ret = spi_sync(spi, &m);
  76. memcpy(p, &r[1], 3);
  77. return ret;
  78. }
  79. static int mc13xxx_spi_write(void *context, const void *data, size_t count)
  80. {
  81. struct device *dev = context;
  82. struct spi_device *spi = to_spi_device(dev);
  83. if (count != 4)
  84. return -ENOTSUPP;
  85. return spi_write(spi, data, count);
  86. }
  87. /*
  88. * We cannot use regmap-spi generic bus implementation here.
  89. * The MC13783 chip will get corrupted if CS signal is deasserted
  90. * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
  91. * has the following errata (DSPhl22960):
  92. * "The CSPI negates SS when the FIFO becomes empty with
  93. * SSCTL= 0. Software cannot guarantee that the FIFO will not
  94. * drain because of higher priority interrupts and the
  95. * non-realtime characteristics of the operating system. As a
  96. * result, the SS will negate before all of the data has been
  97. * transferred to/from the peripheral."
  98. * We workaround this by accessing the SPI controller with a
  99. * single transfert.
  100. */
  101. static struct regmap_bus regmap_mc13xxx_bus = {
  102. .write = mc13xxx_spi_write,
  103. .read = mc13xxx_spi_read,
  104. };
  105. static int mc13xxx_spi_probe(struct spi_device *spi)
  106. {
  107. struct mc13xxx *mc13xxx;
  108. struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev);
  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->dev = &spi->dev;
  116. mutex_init(&mc13xxx->lock);
  117. mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
  118. &spi->dev,
  119. &mc13xxx_regmap_spi_config);
  120. if (IS_ERR(mc13xxx->regmap)) {
  121. ret = PTR_ERR(mc13xxx->regmap);
  122. dev_err(mc13xxx->dev, "Failed to initialize register map: %d\n",
  123. ret);
  124. dev_set_drvdata(&spi->dev, NULL);
  125. return ret;
  126. }
  127. if (spi->dev.of_node) {
  128. const struct of_device_id *of_id =
  129. of_match_device(mc13xxx_dt_ids, &spi->dev);
  130. mc13xxx->variant = of_id->data;
  131. } else {
  132. const struct spi_device_id *id_entry = spi_get_device_id(spi);
  133. mc13xxx->variant = (void *)id_entry->driver_data;
  134. }
  135. return mc13xxx_common_init(mc13xxx, pdata, spi->irq);
  136. }
  137. static int mc13xxx_spi_remove(struct spi_device *spi)
  138. {
  139. struct mc13xxx *mc13xxx = dev_get_drvdata(&spi->dev);
  140. mc13xxx_common_cleanup(mc13xxx);
  141. return 0;
  142. }
  143. static struct spi_driver mc13xxx_spi_driver = {
  144. .id_table = mc13xxx_device_id,
  145. .driver = {
  146. .name = "mc13xxx",
  147. .owner = THIS_MODULE,
  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");