mc13xxx-spi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = MC13XXX_ID_MC13783,
  29. }, {
  30. .name = "mc13892",
  31. .driver_data = MC13XXX_ID_MC13892,
  32. }, {
  33. /* sentinel */
  34. }
  35. };
  36. MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
  37. static const struct of_device_id mc13xxx_dt_ids[] = {
  38. { .compatible = "fsl,mc13783", .data = (void *) MC13XXX_ID_MC13783, },
  39. { .compatible = "fsl,mc13892", .data = (void *) MC13XXX_ID_MC13892, },
  40. { /* sentinel */ }
  41. };
  42. MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
  43. static 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_rw = 1,
  51. };
  52. static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size,
  53. void *val, size_t val_size)
  54. {
  55. unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0};
  56. unsigned char r[4];
  57. unsigned char *p = val;
  58. struct device *dev = context;
  59. struct spi_device *spi = to_spi_device(dev);
  60. struct spi_transfer t = {
  61. .tx_buf = w,
  62. .rx_buf = r,
  63. .len = 4,
  64. };
  65. struct spi_message m;
  66. int ret;
  67. if (val_size != 3 || reg_size != 1)
  68. return -ENOTSUPP;
  69. spi_message_init(&m);
  70. spi_message_add_tail(&t, &m);
  71. ret = spi_sync(spi, &m);
  72. memcpy(p, &r[1], 3);
  73. return ret;
  74. }
  75. static int mc13xxx_spi_write(void *context, const void *data, size_t count)
  76. {
  77. struct device *dev = context;
  78. struct spi_device *spi = to_spi_device(dev);
  79. if (count != 4)
  80. return -ENOTSUPP;
  81. return spi_write(spi, data, count);
  82. }
  83. /*
  84. * We cannot use regmap-spi generic bus implementation here.
  85. * The MC13783 chip will get corrupted if CS signal is deasserted
  86. * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
  87. * has the following errata (DSPhl22960):
  88. * "The CSPI negates SS when the FIFO becomes empty with
  89. * SSCTL= 0. Software cannot guarantee that the FIFO will not
  90. * drain because of higher priority interrupts and the
  91. * non-realtime characteristics of the operating system. As a
  92. * result, the SS will negate before all of the data has been
  93. * transferred to/from the peripheral."
  94. * We workaround this by accessing the SPI controller with a
  95. * single transfert.
  96. */
  97. static struct regmap_bus regmap_mc13xxx_bus = {
  98. .write = mc13xxx_spi_write,
  99. .read = mc13xxx_spi_read,
  100. };
  101. static int mc13xxx_spi_probe(struct spi_device *spi)
  102. {
  103. struct mc13xxx *mc13xxx;
  104. struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev);
  105. int ret;
  106. mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL);
  107. if (!mc13xxx)
  108. return -ENOMEM;
  109. dev_set_drvdata(&spi->dev, mc13xxx);
  110. spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
  111. mc13xxx->dev = &spi->dev;
  112. mutex_init(&mc13xxx->lock);
  113. mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
  114. &spi->dev,
  115. &mc13xxx_regmap_spi_config);
  116. if (IS_ERR(mc13xxx->regmap)) {
  117. ret = PTR_ERR(mc13xxx->regmap);
  118. dev_err(mc13xxx->dev, "Failed to initialize register map: %d\n",
  119. ret);
  120. dev_set_drvdata(&spi->dev, NULL);
  121. return ret;
  122. }
  123. ret = mc13xxx_common_init(mc13xxx, pdata, spi->irq);
  124. if (ret) {
  125. dev_set_drvdata(&spi->dev, NULL);
  126. } else {
  127. const struct spi_device_id *devid =
  128. spi_get_device_id(spi);
  129. if (!devid || devid->driver_data != mc13xxx->ictype)
  130. dev_warn(mc13xxx->dev,
  131. "device id doesn't match auto detection!\n");
  132. }
  133. return ret;
  134. }
  135. static int __devexit mc13xxx_spi_remove(struct spi_device *spi)
  136. {
  137. struct mc13xxx *mc13xxx = dev_get_drvdata(&spi->dev);
  138. mc13xxx_common_cleanup(mc13xxx);
  139. return 0;
  140. }
  141. static struct spi_driver mc13xxx_spi_driver = {
  142. .id_table = mc13xxx_device_id,
  143. .driver = {
  144. .name = "mc13xxx",
  145. .owner = THIS_MODULE,
  146. .of_match_table = mc13xxx_dt_ids,
  147. },
  148. .probe = mc13xxx_spi_probe,
  149. .remove = __devexit_p(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 <u.kleine-koenig@pengutronix.de>");
  163. MODULE_LICENSE("GPL v2");