lis3lv02d_i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (lis3->whoami == WAI_3DLH)
  82. reg |= CTRL1_PM0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
  83. else
  84. reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
  85. return lis3->write(lis3, CTRL_REG1, reg);
  86. }
  87. /* Default axis mapping but it can be overwritten by platform data */
  88. static union axis_conversion lis3lv02d_axis_map =
  89. { .as_array = { LIS3_DEV_X, LIS3_DEV_Y, LIS3_DEV_Z } };
  90. static int __devinit lis3lv02d_i2c_probe(struct i2c_client *client,
  91. const struct i2c_device_id *id)
  92. {
  93. int ret = 0;
  94. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  95. if (pdata) {
  96. if ((pdata->driver_features & LIS3_USE_BLOCK_READ) &&
  97. (i2c_check_functionality(client->adapter,
  98. I2C_FUNC_SMBUS_I2C_BLOCK)))
  99. lis3_dev.blkread = lis3_i2c_blockread;
  100. if (pdata->axis_x)
  101. lis3lv02d_axis_map.x = pdata->axis_x;
  102. if (pdata->axis_y)
  103. lis3lv02d_axis_map.y = pdata->axis_y;
  104. if (pdata->axis_z)
  105. lis3lv02d_axis_map.z = pdata->axis_z;
  106. if (pdata->setup_resources)
  107. ret = pdata->setup_resources();
  108. if (ret)
  109. goto fail;
  110. }
  111. lis3_dev.regulators[0].supply = reg_vdd;
  112. lis3_dev.regulators[1].supply = reg_vdd_io;
  113. ret = regulator_bulk_get(&client->dev,
  114. ARRAY_SIZE(lis3_dev.regulators),
  115. lis3_dev.regulators);
  116. if (ret < 0)
  117. goto fail;
  118. lis3_dev.pdata = pdata;
  119. lis3_dev.bus_priv = client;
  120. lis3_dev.init = lis3_i2c_init;
  121. lis3_dev.read = lis3_i2c_read;
  122. lis3_dev.write = lis3_i2c_write;
  123. lis3_dev.irq = client->irq;
  124. lis3_dev.ac = lis3lv02d_axis_map;
  125. lis3_dev.pm_dev = &client->dev;
  126. i2c_set_clientdata(client, &lis3_dev);
  127. /* Provide power over the init call */
  128. lis3_reg_ctrl(&lis3_dev, LIS3_REG_ON);
  129. ret = lis3lv02d_init_device(&lis3_dev);
  130. lis3_reg_ctrl(&lis3_dev, LIS3_REG_OFF);
  131. if (ret)
  132. goto fail2;
  133. return 0;
  134. fail2:
  135. regulator_bulk_free(ARRAY_SIZE(lis3_dev.regulators),
  136. lis3_dev.regulators);
  137. fail:
  138. if (pdata && pdata->release_resources)
  139. pdata->release_resources();
  140. return ret;
  141. }
  142. static int __devexit lis3lv02d_i2c_remove(struct i2c_client *client)
  143. {
  144. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  145. struct lis3lv02d_platform_data *pdata = client->dev.platform_data;
  146. if (pdata && pdata->release_resources)
  147. pdata->release_resources();
  148. lis3lv02d_joystick_disable(lis3);
  149. lis3lv02d_remove_fs(&lis3_dev);
  150. regulator_bulk_free(ARRAY_SIZE(lis3->regulators),
  151. lis3_dev.regulators);
  152. return 0;
  153. }
  154. #ifdef CONFIG_PM_SLEEP
  155. static int lis3lv02d_i2c_suspend(struct device *dev)
  156. {
  157. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  158. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  159. if (!lis3->pdata || !lis3->pdata->wakeup_flags)
  160. lis3lv02d_poweroff(lis3);
  161. return 0;
  162. }
  163. static int lis3lv02d_i2c_resume(struct device *dev)
  164. {
  165. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  166. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  167. /*
  168. * pm_runtime documentation says that devices should always
  169. * be powered on at resume. Pm_runtime turns them off after system
  170. * wide resume is complete.
  171. */
  172. if (!lis3->pdata || !lis3->pdata->wakeup_flags ||
  173. pm_runtime_suspended(dev))
  174. lis3lv02d_poweron(lis3);
  175. return 0;
  176. }
  177. #endif /* CONFIG_PM_SLEEP */
  178. #ifdef CONFIG_PM_RUNTIME
  179. static int lis3_i2c_runtime_suspend(struct device *dev)
  180. {
  181. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  182. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  183. lis3lv02d_poweroff(lis3);
  184. return 0;
  185. }
  186. static int lis3_i2c_runtime_resume(struct device *dev)
  187. {
  188. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  189. struct lis3lv02d *lis3 = i2c_get_clientdata(client);
  190. lis3lv02d_poweron(lis3);
  191. return 0;
  192. }
  193. #endif /* CONFIG_PM_RUNTIME */
  194. static const struct i2c_device_id lis3lv02d_id[] = {
  195. {"lis3lv02d", 0 },
  196. {"lis331dlh", LIS331DLH},
  197. {}
  198. };
  199. MODULE_DEVICE_TABLE(i2c, lis3lv02d_id);
  200. static const struct dev_pm_ops lis3_pm_ops = {
  201. SET_SYSTEM_SLEEP_PM_OPS(lis3lv02d_i2c_suspend,
  202. lis3lv02d_i2c_resume)
  203. SET_RUNTIME_PM_OPS(lis3_i2c_runtime_suspend,
  204. lis3_i2c_runtime_resume,
  205. NULL)
  206. };
  207. static struct i2c_driver lis3lv02d_i2c_driver = {
  208. .driver = {
  209. .name = DRV_NAME,
  210. .owner = THIS_MODULE,
  211. .pm = &lis3_pm_ops,
  212. },
  213. .probe = lis3lv02d_i2c_probe,
  214. .remove = __devexit_p(lis3lv02d_i2c_remove),
  215. .id_table = lis3lv02d_id,
  216. };
  217. module_i2c_driver(lis3lv02d_i2c_driver);
  218. MODULE_AUTHOR("Nokia Corporation");
  219. MODULE_DESCRIPTION("lis3lv02d I2C interface");
  220. MODULE_LICENSE("GPL");