lis3lv02d_spi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * lis3lv02d_spi - SPI glue layer for lis3lv02d
  3. *
  4. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * publishhed by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/input.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/pm.h>
  19. #include "lis3lv02d.h"
  20. #define DRV_NAME "lis3lv02d_spi"
  21. #define LIS3_SPI_READ 0x80
  22. static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v)
  23. {
  24. struct spi_device *spi = lis3->bus_priv;
  25. int ret = spi_w8r8(spi, reg | LIS3_SPI_READ);
  26. if (ret < 0)
  27. return -EINVAL;
  28. *v = (u8) ret;
  29. return 0;
  30. }
  31. static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val)
  32. {
  33. u8 tmp[2] = { reg, val };
  34. struct spi_device *spi = lis3->bus_priv;
  35. return spi_write(spi, tmp, sizeof(tmp));
  36. }
  37. static int lis3_spi_init(struct lis3lv02d *lis3)
  38. {
  39. u8 reg;
  40. int ret;
  41. /* power up the device */
  42. ret = lis3->read(lis3, CTRL_REG1, &reg);
  43. if (ret < 0)
  44. return ret;
  45. reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
  46. return lis3->write(lis3, CTRL_REG1, reg);
  47. }
  48. static union axis_conversion lis3lv02d_axis_normal =
  49. { .as_array = { 1, 2, 3 } };
  50. static int __devinit lis302dl_spi_probe(struct spi_device *spi)
  51. {
  52. int ret;
  53. spi->bits_per_word = 8;
  54. spi->mode = SPI_MODE_0;
  55. ret = spi_setup(spi);
  56. if (ret < 0)
  57. return ret;
  58. lis3_dev.bus_priv = spi;
  59. lis3_dev.init = lis3_spi_init;
  60. lis3_dev.read = lis3_spi_read;
  61. lis3_dev.write = lis3_spi_write;
  62. lis3_dev.irq = spi->irq;
  63. lis3_dev.ac = lis3lv02d_axis_normal;
  64. lis3_dev.pdata = spi->dev.platform_data;
  65. spi_set_drvdata(spi, &lis3_dev);
  66. return lis3lv02d_init_device(&lis3_dev);
  67. }
  68. static int __devexit lis302dl_spi_remove(struct spi_device *spi)
  69. {
  70. struct lis3lv02d *lis3 = spi_get_drvdata(spi);
  71. lis3lv02d_joystick_disable();
  72. lis3lv02d_poweroff(lis3);
  73. return lis3lv02d_remove_fs(&lis3_dev);
  74. }
  75. #ifdef CONFIG_PM_SLEEP
  76. static int lis3lv02d_spi_suspend(struct device *dev)
  77. {
  78. struct spi_device *spi = to_spi_device(dev);
  79. struct lis3lv02d *lis3 = spi_get_drvdata(spi);
  80. if (!lis3->pdata || !lis3->pdata->wakeup_flags)
  81. lis3lv02d_poweroff(&lis3_dev);
  82. return 0;
  83. }
  84. static int lis3lv02d_spi_resume(struct device *dev)
  85. {
  86. struct spi_device *spi = to_spi_device(dev);
  87. struct lis3lv02d *lis3 = spi_get_drvdata(spi);
  88. if (!lis3->pdata || !lis3->pdata->wakeup_flags)
  89. lis3lv02d_poweron(lis3);
  90. return 0;
  91. }
  92. #endif
  93. static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend,
  94. lis3lv02d_spi_resume);
  95. static struct spi_driver lis302dl_spi_driver = {
  96. .driver = {
  97. .name = DRV_NAME,
  98. .owner = THIS_MODULE,
  99. .pm = &lis3lv02d_spi_pm,
  100. },
  101. .probe = lis302dl_spi_probe,
  102. .remove = __devexit_p(lis302dl_spi_remove),
  103. };
  104. static int __init lis302dl_init(void)
  105. {
  106. return spi_register_driver(&lis302dl_spi_driver);
  107. }
  108. static void __exit lis302dl_exit(void)
  109. {
  110. spi_unregister_driver(&lis302dl_spi_driver);
  111. }
  112. module_init(lis302dl_init);
  113. module_exit(lis302dl_exit);
  114. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  115. MODULE_DESCRIPTION("lis3lv02d SPI glue layer");
  116. MODULE_LICENSE("GPL");
  117. MODULE_ALIAS("spi:" DRV_NAME);