lis3lv02d_i2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <linux/pm_runtime.h>
  32. #include <linux/delay.h>
  33. #include "lis3lv02d.h"
  34. #define DRV_NAME "lis3lv02d_i2c"
  35. static const char reg_vdd[] = "Vdd";
  36. static const char reg_vdd_io[] = "Vdd_IO";
  37. static int lis3_reg_ctrl(struct lis3lv02d *lis3, bool state)
  38. {
  39. int ret;
  40. if (state == LIS3_REG_OFF) {
  41. ret = regulator_bulk_disable(ARRAY_SIZE(lis3->regulators),
  42. lis3->regulators);
  43. } else {
  44. ret = regulator_bulk_enable(ARRAY_SIZE(lis3->regulators),
  45. lis3->regulators);
  46. /* Chip needs time to wakeup. Not mentioned in datasheet */
  47. usleep_range(10000, 20000);
  48. }
  49. return ret;
  50. }
  51. static inline s32 lis3_i2c_write(struct lis3lv02d *lis3, int reg, u8 value)
  52. {
  53. struct i2c_client *c = lis3->bus_priv;
  54. return i2c_smbus_write_byte_data(c, reg, value);
  55. }
  56. static inline s32 lis3_i2c_read(struct lis3lv02d *lis3, int reg, u8 *v)
  57. {
  58. struct i2c_client *c = lis3->bus_priv;
  59. *v = i2c_smbus_read_byte_data(c, reg);
  60. return 0;
  61. }
  62. static inline s32 lis3_i2c_blockread(struct lis3lv02d *lis3, int reg, int len,
  63. u8 *v)
  64. {
  65. struct i2c_client *c = lis3->bus_priv;
  66. reg |= (1 << 7); /* 7th bit enables address auto incrementation */
  67. return i2c_smbus_read_i2c_block_data(c, reg, len, v);
  68. }
  69. static int lis3_i2c_init(struct lis3lv02d *lis3)
  70. {
  71. u8 reg;
  72. int ret;
  73. lis3_reg_ctrl(lis3, LIS3_REG_ON);
  74. lis3->read(lis3, WHO_AM_I, &reg);
  75. if (reg != lis3->whoami)
  76. printk(KERN_ERR "lis3: power on failure\n");
  77. /* power up the device */
  78. ret = lis3->read(lis3, CTRL_REG1, &reg);
  79. if (ret < 0)
  80. return ret;
  81. reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
  82. return lis3->write(lis3, CTRL_REG1, reg);
  83. }
  84. /* Default axis mapping but it can be overwritten by platform data */
  85. static union axis_conversion lis3lv02d_axis_map =
  86. { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
  87. static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
  88. const struct i2c_device_id *id)
  89. {
  90. int ret = 0;
  91. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  92. if (pdata) {
  93. if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
  94. (i2c_check_functionality(client->adapter,
  95. I2C_FUNC_SMBUS_I2C_BLOCK)))
  96. lis3_dev.blkread = lis3_i2c_blockread;
  97. if (pdata->axis_x)
  98. lis3lv02d_axis_map.x = pdata->axis_x;
  99. if (pdata->axis_y)
  100. lis3lv02d_axis_map.y = pdata->axis_y;
  101. if (pdata->axis_z)
  102. lis3lv02d_axis_map.z = pdata->axis_z;
  103. if (pdata->setup_resources)
  104. ret = pdata->setup_resources();
  105. if (ret)
  106. goto fail;
  107. }
  108. lis3_dev.regulators[0].supply = reg_vdd;
  109. lis3_dev.regulators[1].supply = reg_vdd_io;
  110. ret = regulator_bulk_get(&client->dev,
  111. ARRAY_SIZE(lis3_dev.regulators),
  112. lis3_dev.regulators);
  113. if (ret < 0)
  114. goto fail;
  115. lis3_dev.pdata = pdata;
  116. lis3_dev.bus_priv = client;
  117. lis3_dev.init = lis3_i2c_init;
  118. lis3_dev.read = lis3_i2c_read;
  119. lis3_dev.write = lis3_i2c_write;
  120. lis3_dev.irq = client->irq;
  121. lis3_dev.ac = lis3lv02d_axis_map;
  122. lis3_dev.pm_dev = &client->dev;
  123. i2c_set_clientdata(client, &lis3_dev);
  124. /* Provide power over the init call */
  125. lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
  126. ret = lis3lv02d_init_device(&lis3_dev);
  127. lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
  128. if (ret)
  129. goto fail2;
  130. return 0;
  131. fail2:
  132. regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
  133. lis3_dev.regulators);
  134. fail:
  135. if (pdata && pdata->release_resources)
  136. pdata->release_resources();
  137. return ret;
  138. }
  139. static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
  140. {
  141. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  142. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  143. if (pdata && pdata->release_resources)
  144. pdata->release_resources();
  145. lis3lv02d_joystick_disable(lis3);
  146. lis3lv02d_remove_fs(&lis3_dev);
  147. regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
  148. lis3_dev.regulators);
  149. return 0;
  150. }
  151. #ifdef CONFIG_PM_SLEEP
  152. static int lis3lv02d_i2c_suspend(struct device *dev)
  153. {
  154. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  155. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  156. if (!lis3->pdata || !lis3->pdata->wakeup_flags)
  157. lis3lv02d_poweroff(lis3);
  158. return 0;
  159. }
  160. static int lis3lv02d_i2c_resume(struct device *dev)
  161. {
  162. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  163. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  164. /*
  165. * pm_runtime documentation says that devices should always
  166. * be powered on at resume. Pm_runtime turns them off after system
  167. * wide resume is complete.
  168. */
  169. if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
  170. pm_runtime_suspended(dev))
  171. lis3lv02d_poweron(lis3);
  172. return 0;
  173. }
  174. #endif /* CONFIG_PM_SLEEP */
  175. #ifdef CONFIG_PM_RUNTIME
  176. static int lis3_i2c_runtime_suspend(struct device *dev)
  177. {
  178. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  179. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  180. lis3lv02d_poweroff(lis3);
  181. return 0;
  182. }
  183. static int lis3_i2c_runtime_resume(struct device *dev)
  184. {
  185. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  186. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  187. lis3lv02d_poweron(lis3);
  188. return 0;
  189. }
  190. #endif /* CONFIG_PM_RUNTIME */
  191. static const struct i2c_device_id lis3lv02d_id[] = {
  192. {"lis3lv02d", 0 },
  193. {}
  194. };
  195. MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
  196. static const struct dev_pm_ops lis3_pm_ops = {
  197. SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
  198. lis3lv02d_i2c_resume)
  199. SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
  200. lis3_i2c_runtime_resume,
  201. NULL)
  202. };
  203. static struct i2c_driver lis3lv02d_i2c_driver = {
  204. .driver = {
  205. .name = DRV_NAME,
  206. .owner = THIS_MODULE,
  207. .pm = &lis3_pm_ops,
  208. },
  209. .probe = lis3lv02d_i2c_probe,
  210. .remove = __devexit_p(lis3lv02d_i2c_remove),
  211. .id_table = lis3lv02d_id,
  212. };
  213. module_i2c_driver(lis3lv02d_i2c_driver);
  214. MODULE_AUTHOR("Nokia Corporation");
  215. MODULE_DESCRIPTION("lis3lv02d I2C interface");
  216. MODULE_LICENSE("GPL");