ab8500-spi.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/mfd/ab8500.h>
  14. /*
  15. * This funtion writes to any AB8500 registers using
  16. * SPI protocol & before it writes it packs the data
  17. * in the below 24 bit frame format
  18. *
  19. * *|------------------------------------|
  20. * *| 23|22...18|17.......10|9|8|7......0|
  21. * *| r/w bank adr data |
  22. * * ------------------------------------
  23. *
  24. * This function shouldn't be called from interrupt
  25. * context
  26. */
  27. static int ab8500_spi_write(struct ab8500 *ab8500, u16 addr, u8 data)
  28. {
  29. struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
  30. dev);
  31. unsigned long spi_data = addr << 10 | data;
  32. struct spi_transfer xfer;
  33. struct spi_message msg;
  34. ab8500->tx_buf[0] = spi_data;
  35. ab8500->rx_buf[0] = 0;
  36. xfer.tx_buf = ab8500->tx_buf;
  37. xfer.rx_buf = NULL;
  38. xfer.len = sizeof(unsigned long);
  39. spi_message_init(&msg);
  40. spi_message_add_tail(&xfer, &msg);
  41. return spi_sync(spi, &msg);
  42. }
  43. static int ab8500_spi_read(struct ab8500 *ab8500, u16 addr)
  44. {
  45. struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
  46. dev);
  47. unsigned long spi_data = 1 << 23 | addr << 10;
  48. struct spi_transfer xfer;
  49. struct spi_message msg;
  50. int ret;
  51. ab8500->tx_buf[0] = spi_data;
  52. ab8500->rx_buf[0] = 0;
  53. xfer.tx_buf = ab8500->tx_buf;
  54. xfer.rx_buf = ab8500->rx_buf;
  55. xfer.len = sizeof(unsigned long);
  56. spi_message_init(&msg);
  57. spi_message_add_tail(&xfer, &msg);
  58. ret = spi_sync(spi, &msg);
  59. if (!ret)
  60. /*
  61. * Only the 8 lowermost bytes are
  62. * defined with value, the rest may
  63. * vary depending on chip/board noise.
  64. */
  65. ret = ab8500->rx_buf[0] & 0xFFU;
  66. return ret;
  67. }
  68. static int __devinit ab8500_spi_probe(struct spi_device *spi)
  69. {
  70. struct ab8500 *ab8500;
  71. int ret;
  72. spi->bits_per_word = 24;
  73. ret = spi_setup(spi);
  74. if (ret < 0)
  75. return ret;
  76. ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
  77. if (!ab8500)
  78. return -ENOMEM;
  79. ab8500->dev = &spi->dev;
  80. ab8500->irq = spi->irq;
  81. ab8500->read = ab8500_spi_read;
  82. ab8500->write = ab8500_spi_write;
  83. spi_set_drvdata(spi, ab8500);
  84. ret = ab8500_init(ab8500);
  85. if (ret)
  86. kfree(ab8500);
  87. return ret;
  88. }
  89. static int __devexit ab8500_spi_remove(struct spi_device *spi)
  90. {
  91. struct ab8500 *ab8500 = spi_get_drvdata(spi);
  92. ab8500_exit(ab8500);
  93. kfree(ab8500);
  94. return 0;
  95. }
  96. static struct spi_driver ab8500_spi_driver = {
  97. .driver = {
  98. .name = "ab8500-spi",
  99. .owner = THIS_MODULE,
  100. },
  101. .probe = ab8500_spi_probe,
  102. .remove = __devexit_p(ab8500_spi_remove)
  103. };
  104. static int __init ab8500_spi_init(void)
  105. {
  106. return spi_register_driver(&ab8500_spi_driver);
  107. }
  108. subsys_initcall(ab8500_spi_init);
  109. static void __exit ab8500_spi_exit(void)
  110. {
  111. spi_unregister_driver(&ab8500_spi_driver);
  112. }
  113. module_exit(ab8500_spi_exit);
  114. MODULE_AUTHOR("Srinidhi KASAGAR <srinidhi.kasagar@stericsson.com");
  115. MODULE_DESCRIPTION("AB8500 SPI");
  116. MODULE_LICENSE("GPL v2");