lis3lv02d_i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * drivers/hwmon/lis3lv02d_i2c.c
  3. *
  4. * Implements I2C interface for lis3lv02d (STMicroelectronics) accelerometer.
  5. * Driver is based on corresponding SPI driver written by Daniel Mack
  6. * (lis3lv02d_spi.c (C) 2009 Daniel Mack <daniel@caiaq.de> ).
  7. *
  8. * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
  9. *
  10. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  24. * 02110-1301 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/err.h>
  30. #include <linux/i2c.h>
  31. #include "lis3lv02d.h"
  32. #define DRV_NAME "lis3lv02d_i2c"
  33. static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
  34. {
  35. struct i2c_client *c = lis3->bus_priv;
  36. return i2c_smbus_write_byte_data(c, reg, value);
  37. }
  38. static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
  39. {
  40. struct i2c_client *c = lis3->bus_priv;
  41. *v = i2c_smbus_read_byte_data(c, reg);
  42. return 0;
  43. }
  44. static int lis3_i2c_init(struct lis3lv02d *lis3)
  45. {
  46. u8 reg;
  47. int ret;
  48. /* power up the device */
  49. ret = lis3->read(lis3, CTRL_REG1, &reg);
  50. if (ret < 0)
  51. return ret;
  52. reg |= CTRL1_PD0;
  53. return lis3->write(lis3, CTRL_REG1, reg);
  54. }
  55. /* Default axis mapping but it can be overwritten by platform data */
  56. static struct axis_conversion lis3lv02d_axis_map = { LIS3_DEV_X,
  57. LIS3_DEV_Y,
  58. LIS3_DEV_Z };
  59. static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
  60. const struct i2c_device_id *id)
  61. {
  62. int ret = 0;
  63. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  64. if (pdata) {
  65. if (pdata->axis_x)
  66. lis3lv02d_axis_map.x = pdata->axis_x;
  67. if (pdata->axis_y)
  68. lis3lv02d_axis_map.y = pdata->axis_y;
  69. if (pdata->axis_z)
  70. lis3lv02d_axis_map.z = pdata->axis_z;
  71. if (pdata->setup_resources)
  72. ret = pdata->setup_resources();
  73. if (ret)
  74. goto fail;
  75. }
  76. lis3_dev.pdata = pdata;
  77. lis3_dev.bus_priv = client;
  78. lis3_dev.init = lis3_i2c_init;
  79. lis3_dev.read = lis3_i2c_read;
  80. lis3_dev.write = lis3_i2c_write;
  81. lis3_dev.irq = client->irq;
  82. lis3_dev.ac = lis3lv02d_axis_map;
  83. i2c_set_clientdata(client, &lis3_dev);
  84. ret = lis3lv02d_init_device(&lis3_dev);
  85. fail:
  86. return ret;
  87. }
  88. static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
  89. {
  90. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  91. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  92. if (pdata && pdata->release_resources)
  93. pdata->release_resources();
  94. lis3lv02d_joystick_disable();
  95. lis3lv02d_poweroff(lis3);
  96. return lis3lv02d_remove_fs(&lis3_dev);
  97. }
  98. #ifdef CONFIG_PM
  99. static int lis3lv02d_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
  100. {
  101. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  102. if (!lis3->pdata->wakeup_flags)
  103. lis3lv02d_poweroff(lis3);
  104. return 0;
  105. }
  106. static int lis3lv02d_i2c_resume(struct i2c_client *client)
  107. {
  108. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  109. if (!lis3->pdata->wakeup_flags)
  110. lis3lv02d_poweron(lis3);
  111. return 0;
  112. }
  113. static void lis3lv02d_i2c_shutdown(struct i2c_client *client)
  114. {
  115. lis3lv02d_i2c_suspend(client, PMSG_SUSPEND);
  116. }
  117. #else
  118. #define lis3lv02d_i2c_suspend NULL
  119. #define lis3lv02d_i2c_resume NULL
  120. #define lis3lv02d_i2c_shutdown NULL
  121. #endif
  122. static const struct i2c_device_id lis3lv02d_id[] = {
  123. {"lis3lv02d", 0 },
  124. {}
  125. };
  126. MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
  127. static struct i2c_driver lis3lv02d_i2c_driver = {
  128. .driver = {
  129. .name = DRV_NAME,
  130. .owner = THIS_MODULE,
  131. },
  132. .suspend = lis3lv02d_i2c_suspend,
  133. .shutdown = lis3lv02d_i2c_shutdown,
  134. .resume = lis3lv02d_i2c_resume,
  135. .probe = lis3lv02d_i2c_probe,
  136. .remove = __devexit_p(lis3lv02d_i2c_remove),
  137. .id_table = lis3lv02d_id,
  138. };
  139. static int __init lis3lv02d_init(void)
  140. {
  141. return i2c_add_driver(&lis3lv02d_i2c_driver);
  142. }
  143. static void __exit lis3lv02d_exit(void)
  144. {
  145. i2c_del_driver(&lis3lv02d_i2c_driver);
  146. }
  147. MODULE_AUTHOR("Nokia Corporation");
  148. MODULE_DESCRIPTION("lis3lv02d I2C interface");
  149. MODULE_LICENSE("GPL");
  150. module_init(lis3lv02d_init);
  151. module_exit(lis3lv02d_exit);