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