max7301.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/slab.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/max7301.h>
  21. /* A write to the MAX7301 means one message with one transfer */
  22. static int max7301_spi_write(struct device *dev, unsigned int reg,
  23. unsigned int val)
  24. {
  25. struct spi_device *spi = to_spi_device(dev);
  26. u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
  27. return spi_write(spi, (const u8 *)&word, sizeof(word));
  28. }
  29. /* A read from the MAX7301 means two transfers; here, one message each */
  30. static int max7301_spi_read(struct device *dev, unsigned int reg)
  31. {
  32. int ret;
  33. u16 word;
  34. struct spi_device *spi = to_spi_device(dev);
  35. word = 0x8000 | (reg << 8);
  36. ret = spi_write(spi, (const u8 *)&word, sizeof(word));
  37. if (ret)
  38. return ret;
  39. /*
  40. * This relies on the fact, that a transfer with NULL tx_buf shifts out
  41. * zero bytes (=NOOP for MAX7301)
  42. */
  43. ret = spi_read(spi, (u8 *)&word, sizeof(word));
  44. if (ret)
  45. return ret;
  46. return word & 0xff;
  47. }
  48. static int __devinit max7301_probe(struct spi_device *spi)
  49. {
  50. struct max7301 *ts;
  51. int ret;
  52. /* bits_per_word cannot be configured in platform data */
  53. spi->bits_per_word = 16;
  54. ret = spi_setup(spi);
  55. if (ret < 0)
  56. return ret;
  57. ts = kzalloc(sizeof(struct max7301), GFP_KERNEL);
  58. if (!ts)
  59. return -ENOMEM;
  60. ts->read = max7301_spi_read;
  61. ts->write = max7301_spi_write;
  62. ts->dev = &spi->dev;
  63. ret = __max730x_probe(ts);
  64. if (ret)
  65. kfree(ts);
  66. return ret;
  67. }
  68. static int __devexit max7301_remove(struct spi_device *spi)
  69. {
  70. return __max730x_remove(&spi->dev);
  71. }
  72. static const struct spi_device_id max7301_id[] = {
  73. { "max7301", 0 },
  74. { }
  75. };
  76. MODULE_DEVICE_TABLE(spi, max7301_id);
  77. static struct spi_driver max7301_driver = {
  78. .driver = {
  79. .name = "max7301",
  80. .owner = THIS_MODULE,
  81. },
  82. .probe = max7301_probe,
  83. .remove = __devexit_p(max7301_remove),
  84. .id_table = max7301_id,
  85. };
  86. static int __init max7301_init(void)
  87. {
  88. return spi_register_driver(&max7301_driver);
  89. }
  90. /* register after spi postcore initcall and before
  91. * subsys initcalls that may rely on these GPIOs
  92. */
  93. subsys_initcall(max7301_init);
  94. static void __exit max7301_exit(void)
  95. {
  96. spi_unregister_driver(&max7301_driver);
  97. }
  98. module_exit(max7301_exit);
  99. MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
  100. MODULE_LICENSE("GPL v2");
  101. MODULE_DESCRIPTION("MAX7301 GPIO-Expander");