stmpe-spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ST Microelectronics MFD: stmpe's spi client specific driver
  3. *
  4. * Copyright (C) ST Microelectronics SA 2011
  5. *
  6. * License Terms: GNU General Public License, version 2
  7. * Author: Viresh Kumar <vireshk@kernel.org> for ST Microelectronics
  8. */
  9. #include <linux/spi/spi.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/types.h>
  15. #include "stmpe.h"
  16. #define READ_CMD (1 << 7)
  17. static int spi_reg_read(struct stmpe *stmpe, u8 reg)
  18. {
  19. struct spi_device *spi = stmpe->client;
  20. int status = spi_w8r16(spi, reg | READ_CMD);
  21. return (status < 0) ? status : status >> 8;
  22. }
  23. static int spi_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
  24. {
  25. struct spi_device *spi = stmpe->client;
  26. u16 cmd = (val << 8) | reg;
  27. return spi_write(spi, (const u8 *)&cmd, 2);
  28. }
  29. static int spi_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
  30. {
  31. int ret, i;
  32. for (i = 0; i < length; i++) {
  33. ret = spi_reg_read(stmpe, reg + i);
  34. if (ret < 0)
  35. return ret;
  36. *(values + i) = ret;
  37. }
  38. return 0;
  39. }
  40. static int spi_block_write(struct stmpe *stmpe, u8 reg, u8 length,
  41. const u8 *values)
  42. {
  43. int ret = 0, i;
  44. for (i = length; i > 0; i--, reg++) {
  45. ret = spi_reg_write(stmpe, reg, *(values + i - 1));
  46. if (ret < 0)
  47. return ret;
  48. }
  49. return ret;
  50. }
  51. static void spi_init(struct stmpe *stmpe)
  52. {
  53. struct spi_device *spi = stmpe->client;
  54. spi->bits_per_word = 8;
  55. /* This register is only present for stmpe811 */
  56. if (stmpe->variant->id_val == 0x0811)
  57. spi_reg_write(stmpe, STMPE811_REG_SPI_CFG, spi->mode);
  58. if (spi_setup(spi) < 0)
  59. dev_dbg(&spi->dev, "spi_setup failed\n");
  60. }
  61. static struct stmpe_client_info spi_ci = {
  62. .read_byte = spi_reg_read,
  63. .write_byte = spi_reg_write,
  64. .read_block = spi_block_read,
  65. .write_block = spi_block_write,
  66. .init = spi_init,
  67. };
  68. static int
  69. stmpe_spi_probe(struct spi_device *spi)
  70. {
  71. const struct spi_device_id *id = spi_get_device_id(spi);
  72. /* don't exceed max specified rate - 1MHz - Limitation of STMPE */
  73. if (spi->max_speed_hz > 1000000) {
  74. dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
  75. (spi->max_speed_hz/1000));
  76. return -EINVAL;
  77. }
  78. spi_ci.irq = spi->irq;
  79. spi_ci.client = spi;
  80. spi_ci.dev = &spi->dev;
  81. return stmpe_probe(&spi_ci, id->driver_data);
  82. }
  83. static int stmpe_spi_remove(struct spi_device *spi)
  84. {
  85. struct stmpe *stmpe = spi_get_drvdata(spi);
  86. return stmpe_remove(stmpe);
  87. }
  88. static const struct of_device_id stmpe_spi_of_match[] = {
  89. { .compatible = "st,stmpe610", },
  90. { .compatible = "st,stmpe801", },
  91. { .compatible = "st,stmpe811", },
  92. { .compatible = "st,stmpe1601", },
  93. { .compatible = "st,stmpe2401", },
  94. { .compatible = "st,stmpe2403", },
  95. { /* sentinel */ },
  96. };
  97. MODULE_DEVICE_TABLE(of, stmpe_spi_of_match);
  98. static const struct spi_device_id stmpe_spi_id[] = {
  99. { "stmpe610", STMPE610 },
  100. { "stmpe801", STMPE801 },
  101. { "stmpe811", STMPE811 },
  102. { "stmpe1601", STMPE1601 },
  103. { "stmpe2401", STMPE2401 },
  104. { "stmpe2403", STMPE2403 },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(spi, stmpe_id);
  108. static struct spi_driver stmpe_spi_driver = {
  109. .driver = {
  110. .name = "stmpe-spi",
  111. .of_match_table = of_match_ptr(stmpe_spi_of_match),
  112. #ifdef CONFIG_PM
  113. .pm = &stmpe_dev_pm_ops,
  114. #endif
  115. },
  116. .probe = stmpe_spi_probe,
  117. .remove = stmpe_spi_remove,
  118. .id_table = stmpe_spi_id,
  119. };
  120. static int __init stmpe_init(void)
  121. {
  122. return spi_register_driver(&stmpe_spi_driver);
  123. }
  124. subsys_initcall(stmpe_init);
  125. static void __exit stmpe_exit(void)
  126. {
  127. spi_unregister_driver(&stmpe_spi_driver);
  128. }
  129. module_exit(stmpe_exit);
  130. MODULE_LICENSE("GPL v2");
  131. MODULE_DESCRIPTION("STMPE MFD SPI Interface Driver");
  132. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");