hmc6352.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * hmc6352.c - Honeywell Compass Driver
  3. *
  4. * Copyright (C) 2009 Intel Corp
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/sysfs.h>
  30. static DEFINE_MUTEX(compass_mutex);
  31. static int compass_command(struct i2c_client *c, u8 cmd)
  32. {
  33. int ret = i2c_master_send(c, &cmd, 1);
  34. if (ret < 0)
  35. dev_warn(&c->dev, "command '%c' failed.\n", cmd);
  36. return ret;
  37. }
  38. static int compass_store(struct device *dev, const char *buf, size_t count,
  39. const char *map)
  40. {
  41. struct i2c_client *c = to_i2c_client(dev);
  42. int ret;
  43. unsigned long val;
  44. if (strict_strtoul(buf, 10, &val))
  45. return -EINVAL;
  46. if (val >= strlen(map))
  47. return -EINVAL;
  48. mutex_lock(&compass_mutex);
  49. ret = compass_command(c, map[val]);
  50. mutex_unlock(&compass_mutex);
  51. if (ret < 0)
  52. return ret;
  53. return count;
  54. }
  55. static ssize_t compass_calibration_store(struct device *dev,
  56. struct device_attribute *attr, const char *buf, size_t count)
  57. {
  58. return compass_store(dev, buf, count, "EC");
  59. }
  60. static ssize_t compass_power_mode_store(struct device *dev,
  61. struct device_attribute *attr, const char *buf, size_t count)
  62. {
  63. return compass_store(dev, buf, count, "SW");
  64. }
  65. static ssize_t compass_heading_data_show(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct i2c_client *client = to_i2c_client(dev);
  69. unsigned char i2c_data[2];
  70. int ret;
  71. mutex_lock(&compass_mutex);
  72. ret = compass_command(client, 'A');
  73. if (ret != 1) {
  74. mutex_unlock(&compass_mutex);
  75. return ret;
  76. }
  77. msleep(10); /* sending 'A' cmd we need to wait for 7-10 millisecs */
  78. ret = i2c_master_recv(client, i2c_data, 2);
  79. mutex_unlock(&compass_mutex);
  80. if (ret < 0) {
  81. dev_warn(dev, "i2c read data cmd failed\n");
  82. return ret;
  83. }
  84. ret = (i2c_data[0] << 8) | i2c_data[1];
  85. return sprintf(buf, "%d.%d\n", ret/10, ret%10);
  86. }
  87. static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL);
  88. static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store);
  89. static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store);
  90. static struct attribute *mid_att_compass[] = {
  91. &dev_attr_heading0_input.attr,
  92. &dev_attr_calibration.attr,
  93. &dev_attr_power_state.attr,
  94. NULL
  95. };
  96. static const struct attribute_group m_compass_gr = {
  97. .name = "hmc6352",
  98. .attrs = mid_att_compass
  99. };
  100. static int hmc6352_probe(struct i2c_client *client,
  101. const struct i2c_device_id *id)
  102. {
  103. int res;
  104. res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
  105. if (res) {
  106. dev_err(&client->dev, "device_create_file failed\n");
  107. return res;
  108. }
  109. dev_info(&client->dev, "%s HMC6352 compass chip found\n",
  110. client->name);
  111. return 0;
  112. }
  113. static int hmc6352_remove(struct i2c_client *client)
  114. {
  115. sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
  116. return 0;
  117. }
  118. static struct i2c_device_id hmc6352_id[] = {
  119. { "hmc6352", 0 },
  120. { }
  121. };
  122. MODULE_DEVICE_TABLE(i2c, hmc6352_id);
  123. static struct i2c_driver hmc6352_driver = {
  124. .driver = {
  125. .name = "hmc6352",
  126. },
  127. .probe = hmc6352_probe,
  128. .remove = hmc6352_remove,
  129. .id_table = hmc6352_id,
  130. };
  131. static int __init sensor_hmc6352_init(void)
  132. {
  133. return i2c_add_driver(&hmc6352_driver);
  134. }
  135. static void __exit sensor_hmc6352_exit(void)
  136. {
  137. i2c_del_driver(&hmc6352_driver);
  138. }
  139. module_init(sensor_hmc6352_init);
  140. module_exit(sensor_hmc6352_exit);
  141. MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
  142. MODULE_DESCRIPTION("hmc6352 Compass Driver");
  143. MODULE_LICENSE("GPL v2");