lis3lv02d_i2c.c 6.7 KB

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