max7301.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * drivers/gpio/max7301.c
  3. *
  4. * Copyright (C) 2006 Juergen Beisert, Pengutronix
  5. * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
  6. * Copyright (C) 2009 Wolfram Sang, Pengutronix
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Check max730x.c for further details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/mutex.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/max7301.h>
  20. /* A write to the MAX7301 means one message with one transfer */
  21. static int max7301_spi_write(struct device *dev, unsigned int reg,
  22. unsigned int val)
  23. {
  24. struct spi_device *spi = to_spi_device(dev);
  25. u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
  26. return spi_write(spi, (const u8 *)&word, sizeof(word));
  27. }
  28. /* A read from the MAX7301 means two transfers; here, one message each */
  29. static int max7301_spi_read(struct device *dev, unsigned int reg)
  30. {
  31. int ret;
  32. u16 word;
  33. struct spi_device *spi = to_spi_device(dev);
  34. word = 0x8000 | (reg << 8);
  35. ret = spi_write(spi, (const u8 *)&word, sizeof(word));
  36. if (ret)
  37. return ret;
  38. /*
  39. * This relies on the fact, that a transfer with NULL tx_buf shifts out
  40. * zero bytes (=NOOP for MAX7301)
  41. */
  42. ret = spi_read(spi, (u8 *)&word, sizeof(word));
  43. if (ret)
  44. return ret;
  45. return word & 0xff;
  46. }
  47. static int __devinit max7301_probe(struct spi_device *spi)
  48. {
  49. struct max7301 *ts;
  50. int ret;
  51. /* bits_per_word cannot be configured in platform data */
  52. spi->bits_per_word = 16;
  53. ret = spi_setup(spi);
  54. if (ret < 0)
  55. return ret;
  56. ts = kzalloc(sizeof(struct max7301), GFP_KERNEL);
  57. if (!ts)
  58. return -ENOMEM;
  59. ts->read = max7301_spi_read;
  60. ts->write = max7301_spi_write;
  61. ts->dev = &spi->dev;
  62. ret = __max730x_probe(ts);
  63. if (ret)
  64. kfree(ts);
  65. return ret;
  66. }
  67. static int __devexit max7301_remove(struct spi_device *spi)
  68. {
  69. return __max730x_remove(&spi->dev);
  70. }
  71. static const struct spi_device_id max7301_id[] = {
  72. { "max7301", 0 },
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE(spi, max7301_id);
  76. static struct spi_driver max7301_driver = {
  77. .driver = {
  78. .name = "max7301",
  79. .owner = THIS_MODULE,
  80. },
  81. .probe = max7301_probe,
  82. .remove = __devexit_p(max7301_remove),
  83. .id_table = max7301_id,
  84. };
  85. static int __init max7301_init(void)
  86. {
  87. return spi_register_driver(&max7301_driver);
  88. }
  89. /* register after spi postcore initcall and before
  90. * subsys initcalls that may rely on these GPIOs
  91. */
  92. subsys_initcall(max7301_init);
  93. static void __exit max7301_exit(void)
  94. {
  95. spi_unregister_driver(&max7301_driver);
  96. }
  97. module_exit(max7301_exit);
  98. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  99. MODULE_LICENSE("GPL v2");
  100. MODULE_DESCRIPTION("MAX7301 GPIO-Expander");