hmc6352.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * hmc6352.c - Honeywell Compass Driver
  4. *
  5. * Copyright (C) 2009 Intel Corp
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/nospec.h>
  18. static DEFINE_MUTEX(compass_mutex);
  19. static int compass_command(struct i2c_client *c, u8 cmd)
  20. {
  21. int ret = i2c_master_send(c, &cmd, 1);
  22. if (ret < 0)
  23. dev_warn(&c->dev, "command '%c' failed.\n", cmd);
  24. return ret;
  25. }
  26. static int compass_store(struct device *dev, const char *buf, size_t count,
  27. const char *map)
  28. {
  29. struct i2c_client *c = to_i2c_client(dev);
  30. int ret;
  31. unsigned long val;
  32. ret = kstrtoul(buf, 10, &val);
  33. if (ret)
  34. return ret;
  35. if (val >= strlen(map))
  36. return -EINVAL;
  37. val = array_index_nospec(val, strlen(map));
  38. mutex_lock(&compass_mutex);
  39. ret = compass_command(c, map[val]);
  40. mutex_unlock(&compass_mutex);
  41. if (ret < 0)
  42. return ret;
  43. return count;
  44. }
  45. static ssize_t compass_calibration_store(struct device *dev,
  46. struct device_attribute *attr, const char *buf, size_t count)
  47. {
  48. return compass_store(dev, buf, count, "EC");
  49. }
  50. static ssize_t compass_power_mode_store(struct device *dev,
  51. struct device_attribute *attr, const char *buf, size_t count)
  52. {
  53. return compass_store(dev, buf, count, "SW");
  54. }
  55. static ssize_t compass_heading_data_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct i2c_client *client = to_i2c_client(dev);
  59. unsigned char i2c_data[2];
  60. int ret;
  61. mutex_lock(&compass_mutex);
  62. ret = compass_command(client, 'A');
  63. if (ret != 1) {
  64. mutex_unlock(&compass_mutex);
  65. return ret;
  66. }
  67. msleep(10); /* sending 'A' cmd we need to wait for 7-10 millisecs */
  68. ret = i2c_master_recv(client, i2c_data, 2);
  69. mutex_unlock(&compass_mutex);
  70. if (ret < 0) {
  71. dev_warn(dev, "i2c read data cmd failed\n");
  72. return ret;
  73. }
  74. ret = (i2c_data[0] << 8) | i2c_data[1];
  75. return sprintf(buf, "%d.%d\n", ret/10, ret%10);
  76. }
  77. static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL);
  78. static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store);
  79. static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store);
  80. static struct attribute *mid_att_compass[] = {
  81. &dev_attr_heading0_input.attr,
  82. &dev_attr_calibration.attr,
  83. &dev_attr_power_state.attr,
  84. NULL
  85. };
  86. static const struct attribute_group m_compass_gr = {
  87. .name = "hmc6352",
  88. .attrs = mid_att_compass
  89. };
  90. static int hmc6352_probe(struct i2c_client *client,
  91. const struct i2c_device_id *id)
  92. {
  93. int res;
  94. res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
  95. if (res) {
  96. dev_err(&client->dev, "device_create_file failed\n");
  97. return res;
  98. }
  99. dev_info(&client->dev, "%s HMC6352 compass chip found\n",
  100. client->name);
  101. return 0;
  102. }
  103. static int hmc6352_remove(struct i2c_client *client)
  104. {
  105. sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
  106. return 0;
  107. }
  108. static const struct i2c_device_id hmc6352_id[] = {
  109. { "hmc6352", 0 },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(i2c, hmc6352_id);
  113. static struct i2c_driver hmc6352_driver = {
  114. .driver = {
  115. .name = "hmc6352",
  116. },
  117. .probe = hmc6352_probe,
  118. .remove = hmc6352_remove,
  119. .id_table = hmc6352_id,
  120. };
  121. module_i2c_driver(hmc6352_driver);
  122. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
  123. MODULE_DESCRIPTION("hmc6352 Compass Driver");
  124. MODULE_LICENSE("GPL v2");