lis3lv02d_i2c.c 7.4 KB

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