lis3lv02d_i2c.c 7.2 KB

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