lis3lv02d_i2c.c 7.4 KB

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