cma3000_d0x_i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Implements I2C interface for VTI CMA300_D0x Accelerometer driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. * Author: Hemanth V <hemanthv@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/input/cma3000.h>
  22. #include "cma3000_d0x.h"
  23. static int cma3000_i2c_set(struct device *dev,
  24. u8 reg, u8 val, char *msg)
  25. {
  26. struct i2c_client *client = to_i2c_client(dev);
  27. int ret;
  28. ret = i2c_smbus_write_byte_data(client, reg, val);
  29. if (ret < 0)
  30. dev_err(&client->dev,
  31. "%s failed (%s, %d)\n", __func__, msg, ret);
  32. return ret;
  33. }
  34. static int cma3000_i2c_read(struct device *dev, u8 reg, char *msg)
  35. {
  36. struct i2c_client *client = to_i2c_client(dev);
  37. int ret;
  38. ret = i2c_smbus_read_byte_data(client, reg);
  39. if (ret < 0)
  40. dev_err(&client->dev,
  41. "%s failed (%s, %d)\n", __func__, msg, ret);
  42. return ret;
  43. }
  44. static const struct cma3000_bus_ops cma3000_i2c_bops = {
  45. .bustype = BUS_I2C,
  46. #define CMA3000_BUSI2C (0 << 4)
  47. .ctrl_mod = CMA3000_BUSI2C,
  48. .read = cma3000_i2c_read,
  49. .write = cma3000_i2c_set,
  50. };
  51. static int cma3000_i2c_probe(struct i2c_client *client,
  52. const struct i2c_device_id *id)
  53. {
  54. struct cma3000_accl_data *data;
  55. data = cma3000_init(&client->dev, client->irq, &cma3000_i2c_bops);
  56. if (IS_ERR(data))
  57. return PTR_ERR(data);
  58. i2c_set_clientdata(client, data);
  59. return 0;
  60. }
  61. static int cma3000_i2c_remove(struct i2c_client *client)
  62. {
  63. struct cma3000_accl_data *data = i2c_get_clientdata(client);
  64. cma3000_exit(data);
  65. return 0;
  66. }
  67. #ifdef CONFIG_PM
  68. static int cma3000_i2c_suspend(struct device *dev)
  69. {
  70. struct i2c_client *client = to_i2c_client(dev);
  71. struct cma3000_accl_data *data = i2c_get_clientdata(client);
  72. cma3000_suspend(data);
  73. return 0;
  74. }
  75. static int cma3000_i2c_resume(struct device *dev)
  76. {
  77. struct i2c_client *client = to_i2c_client(dev);
  78. struct cma3000_accl_data *data = i2c_get_clientdata(client);
  79. cma3000_resume(data);
  80. return 0;
  81. }
  82. static const struct dev_pm_ops cma3000_i2c_pm_ops = {
  83. .suspend = cma3000_i2c_suspend,
  84. .resume = cma3000_i2c_resume,
  85. };
  86. #endif
  87. static const struct i2c_device_id cma3000_i2c_id[] = {
  88. { "cma3000_d01", 0 },
  89. { },
  90. };
  91. MODULE_DEVICE_TABLE(i2c, cma3000_i2c_id);
  92. static struct i2c_driver cma3000_i2c_driver = {
  93. .probe = cma3000_i2c_probe,
  94. .remove = cma3000_i2c_remove,
  95. .id_table = cma3000_i2c_id,
  96. .driver = {
  97. .name = "cma3000_i2c_accl",
  98. #ifdef CONFIG_PM
  99. .pm = &cma3000_i2c_pm_ops,
  100. #endif
  101. },
  102. };
  103. module_i2c_driver(cma3000_i2c_driver);
  104. MODULE_DESCRIPTION("CMA3000-D0x Accelerometer I2C Driver");
  105. MODULE_LICENSE("GPL");
  106. MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");