tps65912-spi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * tps65912-spi.c -- SPI access for TI TPS65912x PMIC
  3. *
  4. * Copyright 2011 Texas Instruments Inc.
  5. *
  6. * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This driver is based on wm8350 implementation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/gpio.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/tps65912.h>
  23. static int tps65912_spi_write(struct tps65912 *tps65912, u8 addr,
  24. int bytes, void *src)
  25. {
  26. struct spi_device *spi = tps65912->control_data;
  27. u8 *data = (u8 *) src;
  28. int ret;
  29. /* bit 23 is the read/write bit */
  30. unsigned long spi_data = 1 << 23 | addr << 15 | *data;
  31. struct spi_transfer xfer;
  32. struct spi_message msg;
  33. u32 tx_buf, rx_buf;
  34. tx_buf = spi_data;
  35. rx_buf = 0;
  36. xfer.tx_buf = &tx_buf;
  37. xfer.rx_buf = NULL;
  38. xfer.len = sizeof(unsigned long);
  39. xfer.bits_per_word = 24;
  40. spi_message_init(&msg);
  41. spi_message_add_tail(&xfer, &msg);
  42. ret = spi_sync(spi, &msg);
  43. return ret;
  44. }
  45. static int tps65912_spi_read(struct tps65912 *tps65912, u8 addr,
  46. int bytes, void *dest)
  47. {
  48. struct spi_device *spi = tps65912->control_data;
  49. /* bit 23 is the read/write bit */
  50. unsigned long spi_data = 0 << 23 | addr << 15;
  51. struct spi_transfer xfer;
  52. struct spi_message msg;
  53. int ret;
  54. u8 *data = (u8 *) dest;
  55. u32 tx_buf, rx_buf;
  56. tx_buf = spi_data;
  57. rx_buf = 0;
  58. xfer.tx_buf = &tx_buf;
  59. xfer.rx_buf = &rx_buf;
  60. xfer.len = sizeof(unsigned long);
  61. xfer.bits_per_word = 24;
  62. spi_message_init(&msg);
  63. spi_message_add_tail(&xfer, &msg);
  64. if (spi == NULL)
  65. return 0;
  66. ret = spi_sync(spi, &msg);
  67. if (ret == 0)
  68. *data = (u8) (rx_buf & 0xFF);
  69. return ret;
  70. }
  71. static int __devinit tps65912_spi_probe(struct spi_device *spi)
  72. {
  73. struct tps65912 *tps65912;
  74. tps65912 = kzalloc(sizeof(struct tps65912), GFP_KERNEL);
  75. if (tps65912 == NULL)
  76. return -ENOMEM;
  77. tps65912->dev = &spi->dev;
  78. tps65912->control_data = spi;
  79. tps65912->read = tps65912_spi_read;
  80. tps65912->write = tps65912_spi_write;
  81. spi_set_drvdata(spi, tps65912);
  82. return tps65912_device_init(tps65912);
  83. }
  84. static int __devexit tps65912_spi_remove(struct spi_device *spi)
  85. {
  86. struct tps65912 *tps65912 = spi_get_drvdata(spi);
  87. tps65912_device_exit(tps65912);
  88. return 0;
  89. }
  90. static struct spi_driver tps65912_spi_driver = {
  91. .driver = {
  92. .name = "tps65912",
  93. .owner = THIS_MODULE,
  94. },
  95. .probe = tps65912_spi_probe,
  96. .remove = __devexit_p(tps65912_spi_remove),
  97. };
  98. static int __init tps65912_spi_init(void)
  99. {
  100. int ret;
  101. ret = spi_register_driver(&tps65912_spi_driver);
  102. if (ret != 0)
  103. pr_err("Failed to register TPS65912 SPI driver: %d\n", ret);
  104. return 0;
  105. }
  106. /* init early so consumer devices can complete system boot */
  107. subsys_initcall(tps65912_spi_init);
  108. static void __exit tps65912_spi_exit(void)
  109. {
  110. spi_unregister_driver(&tps65912_spi_driver);
  111. }
  112. module_exit(tps65912_spi_exit);
  113. MODULE_AUTHOR("Margarita Olaya <magi@slimlogic.co.uk>");
  114. MODULE_DESCRIPTION("SPI support for TPS65912 chip family mfd");
  115. MODULE_LICENSE("GPL");