0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/i2c.h>
0015 #include <linux/err.h>
0016 #include <linux/delay.h>
0017 #include <linux/sysfs.h>
0018 #include <linux/nospec.h>
0019
0020 static DEFINE_MUTEX(compass_mutex);
0021
0022 static int compass_command(struct i2c_client *c, u8 cmd)
0023 {
0024 int ret = i2c_master_send(c, &cmd, 1);
0025 if (ret < 0)
0026 dev_warn(&c->dev, "command '%c' failed.\n", cmd);
0027 return ret;
0028 }
0029
0030 static int compass_store(struct device *dev, const char *buf, size_t count,
0031 const char *map)
0032 {
0033 struct i2c_client *c = to_i2c_client(dev);
0034 int ret;
0035 unsigned long val;
0036
0037 ret = kstrtoul(buf, 10, &val);
0038 if (ret)
0039 return ret;
0040 if (val >= strlen(map))
0041 return -EINVAL;
0042 val = array_index_nospec(val, strlen(map));
0043 mutex_lock(&compass_mutex);
0044 ret = compass_command(c, map[val]);
0045 mutex_unlock(&compass_mutex);
0046 if (ret < 0)
0047 return ret;
0048 return count;
0049 }
0050
0051 static ssize_t compass_calibration_store(struct device *dev,
0052 struct device_attribute *attr, const char *buf, size_t count)
0053 {
0054 return compass_store(dev, buf, count, "EC");
0055 }
0056
0057 static ssize_t compass_power_mode_store(struct device *dev,
0058 struct device_attribute *attr, const char *buf, size_t count)
0059 {
0060 return compass_store(dev, buf, count, "SW");
0061 }
0062
0063 static ssize_t compass_heading_data_show(struct device *dev,
0064 struct device_attribute *attr, char *buf)
0065 {
0066 struct i2c_client *client = to_i2c_client(dev);
0067 unsigned char i2c_data[2];
0068 int ret;
0069
0070 mutex_lock(&compass_mutex);
0071 ret = compass_command(client, 'A');
0072 if (ret != 1) {
0073 mutex_unlock(&compass_mutex);
0074 return ret;
0075 }
0076 msleep(10);
0077 ret = i2c_master_recv(client, i2c_data, 2);
0078 mutex_unlock(&compass_mutex);
0079 if (ret < 0) {
0080 dev_warn(dev, "i2c read data cmd failed\n");
0081 return ret;
0082 }
0083 ret = (i2c_data[0] << 8) | i2c_data[1];
0084 return sprintf(buf, "%d.%d\n", ret/10, ret%10);
0085 }
0086
0087
0088 static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL);
0089 static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store);
0090 static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store);
0091
0092 static struct attribute *mid_att_compass[] = {
0093 &dev_attr_heading0_input.attr,
0094 &dev_attr_calibration.attr,
0095 &dev_attr_power_state.attr,
0096 NULL
0097 };
0098
0099 static const struct attribute_group m_compass_gr = {
0100 .name = "hmc6352",
0101 .attrs = mid_att_compass
0102 };
0103
0104 static int hmc6352_probe(struct i2c_client *client,
0105 const struct i2c_device_id *id)
0106 {
0107 int res;
0108
0109 res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
0110 if (res) {
0111 dev_err(&client->dev, "device_create_file failed\n");
0112 return res;
0113 }
0114 dev_info(&client->dev, "%s HMC6352 compass chip found\n",
0115 client->name);
0116 return 0;
0117 }
0118
0119 static int hmc6352_remove(struct i2c_client *client)
0120 {
0121 sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
0122 return 0;
0123 }
0124
0125 static const struct i2c_device_id hmc6352_id[] = {
0126 { "hmc6352", 0 },
0127 { }
0128 };
0129
0130 MODULE_DEVICE_TABLE(i2c, hmc6352_id);
0131
0132 static struct i2c_driver hmc6352_driver = {
0133 .driver = {
0134 .name = "hmc6352",
0135 },
0136 .probe = hmc6352_probe,
0137 .remove = hmc6352_remove,
0138 .id_table = hmc6352_id,
0139 };
0140
0141 module_i2c_driver(hmc6352_driver);
0142
0143 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
0144 MODULE_DESCRIPTION("hmc6352 Compass Driver");
0145 MODULE_LICENSE("GPL v2");