blob: 9cde4c5bfba409c7a1460523fc253834135843c0 [file] [log] [blame]
Kalhan Trisalcfa3b242010-08-09 17:21:05 -07001/*
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
24#include <linux/module.h>
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070025#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/err.h>
28#include <linux/delay.h>
29#include <linux/sysfs.h>
Gustavo A. R. Silva76b61ea2018-08-15 10:50:41 -050030#include <linux/nospec.h>
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070031
32static DEFINE_MUTEX(compass_mutex);
33
34static int compass_command(struct i2c_client *c, u8 cmd)
35{
36 int ret = i2c_master_send(c, &cmd, 1);
37 if (ret < 0)
38 dev_warn(&c->dev, "command '%c' failed.\n", cmd);
39 return ret;
40}
41
42static int compass_store(struct device *dev, const char *buf, size_t count,
43 const char *map)
44{
45 struct i2c_client *c = to_i2c_client(dev);
46 int ret;
47 unsigned long val;
48
Jingoo Hanf7b41272013-06-04 13:15:16 +090049 ret = kstrtoul(buf, 10, &val);
50 if (ret)
51 return ret;
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070052 if (val >= strlen(map))
53 return -EINVAL;
Gustavo A. R. Silva76b61ea2018-08-15 10:50:41 -050054 val = array_index_nospec(val, strlen(map));
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070055 mutex_lock(&compass_mutex);
56 ret = compass_command(c, map[val]);
57 mutex_unlock(&compass_mutex);
58 if (ret < 0)
59 return ret;
60 return count;
61}
62
63static ssize_t compass_calibration_store(struct device *dev,
64 struct device_attribute *attr, const char *buf, size_t count)
65{
66 return compass_store(dev, buf, count, "EC");
67}
68
69static ssize_t compass_power_mode_store(struct device *dev,
70 struct device_attribute *attr, const char *buf, size_t count)
71{
72 return compass_store(dev, buf, count, "SW");
73}
74
75static ssize_t compass_heading_data_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
77{
78 struct i2c_client *client = to_i2c_client(dev);
79 unsigned char i2c_data[2];
Axel Lin6f7d4852011-03-22 16:34:00 -070080 int ret;
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070081
82 mutex_lock(&compass_mutex);
83 ret = compass_command(client, 'A');
84 if (ret != 1) {
85 mutex_unlock(&compass_mutex);
86 return ret;
87 }
88 msleep(10); /* sending 'A' cmd we need to wait for 7-10 millisecs */
89 ret = i2c_master_recv(client, i2c_data, 2);
90 mutex_unlock(&compass_mutex);
Axel Lin6f7d4852011-03-22 16:34:00 -070091 if (ret < 0) {
Kalhan Trisalcfa3b242010-08-09 17:21:05 -070092 dev_warn(dev, "i2c read data cmd failed\n");
93 return ret;
94 }
95 ret = (i2c_data[0] << 8) | i2c_data[1];
96 return sprintf(buf, "%d.%d\n", ret/10, ret%10);
97}
98
99
100static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL);
101static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store);
102static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store);
103
104static struct attribute *mid_att_compass[] = {
105 &dev_attr_heading0_input.attr,
106 &dev_attr_calibration.attr,
107 &dev_attr_power_state.attr,
108 NULL
109};
110
111static const struct attribute_group m_compass_gr = {
112 .name = "hmc6352",
113 .attrs = mid_att_compass
114};
115
116static int hmc6352_probe(struct i2c_client *client,
117 const struct i2c_device_id *id)
118{
119 int res;
120
121 res = sysfs_create_group(&client->dev.kobj, &m_compass_gr);
122 if (res) {
123 dev_err(&client->dev, "device_create_file failed\n");
124 return res;
125 }
126 dev_info(&client->dev, "%s HMC6352 compass chip found\n",
127 client->name);
128 return 0;
129}
130
131static int hmc6352_remove(struct i2c_client *client)
132{
133 sysfs_remove_group(&client->dev.kobj, &m_compass_gr);
134 return 0;
135}
136
137static struct i2c_device_id hmc6352_id[] = {
138 { "hmc6352", 0 },
139 { }
140};
141
142MODULE_DEVICE_TABLE(i2c, hmc6352_id);
143
144static struct i2c_driver hmc6352_driver = {
145 .driver = {
146 .name = "hmc6352",
147 },
148 .probe = hmc6352_probe,
149 .remove = hmc6352_remove,
150 .id_table = hmc6352_id,
151};
152
Axel Lina64fe2e2012-01-22 15:36:45 +0800153module_i2c_driver(hmc6352_driver);
Kalhan Trisalcfa3b242010-08-09 17:21:05 -0700154
155MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com");
156MODULE_DESCRIPTION("hmc6352 Compass Driver");
157MODULE_LICENSE("GPL v2");