gpio-max7301.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2006 Juergen Beisert, Pengutronix
  4. * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
  5. * Copyright (C) 2009 Wolfram Sang, Pengutronix
  6. *
  7. * Check max730x.c for further details.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/spi/max7301.h>
  16. /* A write to the MAX7301 means one message with one transfer */
  17. static int max7301_spi_write(struct device *dev, unsigned int reg,
  18. unsigned int val)
  19. {
  20. struct spi_device *spi = to_spi_device(dev);
  21. u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
  22. return spi_write_then_read(spi, &word, sizeof(word), NULL, 0);
  23. }
  24. /* A read from the MAX7301 means two transfers; here, one message each */
  25. static int max7301_spi_read(struct device *dev, unsigned int reg)
  26. {
  27. int ret;
  28. u16 word;
  29. struct spi_device *spi = to_spi_device(dev);
  30. word = 0x8000 | (reg << 8);
  31. ret = spi_write_then_read(spi, &word, sizeof(word), &word,
  32. sizeof(word));
  33. if (ret)
  34. return ret;
  35. return word & 0xff;
  36. }
  37. static int max7301_probe(struct spi_device *spi)
  38. {
  39. struct max7301 *ts;
  40. int ret;
  41. /* bits_per_word cannot be configured in platform data */
  42. spi->bits_per_word = 16;
  43. ret = spi_setup(spi);
  44. if (ret < 0)
  45. return ret;
  46. ts = devm_kzalloc(&spi->dev, sizeof(struct max7301), GFP_KERNEL);
  47. if (!ts)
  48. return -ENOMEM;
  49. ts->read = max7301_spi_read;
  50. ts->write = max7301_spi_write;
  51. ts->dev = &spi->dev;
  52. ret = __max730x_probe(ts);
  53. return ret;
  54. }
  55. static void max7301_remove(struct spi_device *spi)
  56. {
  57. __max730x_remove(&spi->dev);
  58. }
  59. static const struct spi_device_id max7301_id[] = {
  60. { "max7301", 0 },
  61. { }
  62. };
  63. MODULE_DEVICE_TABLE(spi, max7301_id);
  64. static struct spi_driver max7301_driver = {
  65. .driver = {
  66. .name = "max7301",
  67. },
  68. .probe = max7301_probe,
  69. .remove = max7301_remove,
  70. .id_table = max7301_id,
  71. };
  72. static int __init max7301_init(void)
  73. {
  74. return spi_register_driver(&max7301_driver);
  75. }
  76. /* register after spi postcore initcall and before
  77. * subsys initcalls that may rely on these GPIOs
  78. */
  79. subsys_initcall(max7301_init);
  80. static void __exit max7301_exit(void)
  81. {
  82. spi_unregister_driver(&max7301_driver);
  83. }
  84. module_exit(max7301_exit);
  85. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  86. MODULE_LICENSE("GPL v2");
  87. MODULE_DESCRIPTION("MAX7301 GPIO-Expander");