Commit | Line | Data |
---|---|---|
cfa3b24c KT |
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 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/i2c.h> | |
28 | #include <linux/err.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/sysfs.h> | |
31 | ||
32 | static DEFINE_MUTEX(compass_mutex); | |
33 | ||
34 | static 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 | ||
42 | static 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 | ||
f7b41276 JH |
49 | ret = kstrtoul(buf, 10, &val); |
50 | if (ret) | |
51 | return ret; | |
cfa3b24c KT |
52 | if (val >= strlen(map)) |
53 | return -EINVAL; | |
54 | mutex_lock(&compass_mutex); | |
55 | ret = compass_command(c, map[val]); | |
56 | mutex_unlock(&compass_mutex); | |
57 | if (ret < 0) | |
58 | return ret; | |
59 | return count; | |
60 | } | |
61 | ||
62 | static ssize_t compass_calibration_store(struct device *dev, | |
63 | struct device_attribute *attr, const char *buf, size_t count) | |
64 | { | |
65 | return compass_store(dev, buf, count, "EC"); | |
66 | } | |
67 | ||
68 | static ssize_t compass_power_mode_store(struct device *dev, | |
69 | struct device_attribute *attr, const char *buf, size_t count) | |
70 | { | |
71 | return compass_store(dev, buf, count, "SW"); | |
72 | } | |
73 | ||
74 | static ssize_t compass_heading_data_show(struct device *dev, | |
75 | struct device_attribute *attr, char *buf) | |
76 | { | |
77 | struct i2c_client *client = to_i2c_client(dev); | |
78 | unsigned char i2c_data[2]; | |
6f7d485e | 79 | int ret; |
cfa3b24c KT |
80 | |
81 | mutex_lock(&compass_mutex); | |
82 | ret = compass_command(client, 'A'); | |
83 | if (ret != 1) { | |
84 | mutex_unlock(&compass_mutex); | |
85 | return ret; | |
86 | } | |
87 | msleep(10); /* sending 'A' cmd we need to wait for 7-10 millisecs */ | |
88 | ret = i2c_master_recv(client, i2c_data, 2); | |
89 | mutex_unlock(&compass_mutex); | |
6f7d485e | 90 | if (ret < 0) { |
cfa3b24c KT |
91 | dev_warn(dev, "i2c read data cmd failed\n"); |
92 | return ret; | |
93 | } | |
94 | ret = (i2c_data[0] << 8) | i2c_data[1]; | |
95 | return sprintf(buf, "%d.%d\n", ret/10, ret%10); | |
96 | } | |
97 | ||
98 | ||
99 | static DEVICE_ATTR(heading0_input, S_IRUGO, compass_heading_data_show, NULL); | |
100 | static DEVICE_ATTR(calibration, S_IWUSR, NULL, compass_calibration_store); | |
101 | static DEVICE_ATTR(power_state, S_IWUSR, NULL, compass_power_mode_store); | |
102 | ||
103 | static struct attribute *mid_att_compass[] = { | |
104 | &dev_attr_heading0_input.attr, | |
105 | &dev_attr_calibration.attr, | |
106 | &dev_attr_power_state.attr, | |
107 | NULL | |
108 | }; | |
109 | ||
110 | static const struct attribute_group m_compass_gr = { | |
111 | .name = "hmc6352", | |
112 | .attrs = mid_att_compass | |
113 | }; | |
114 | ||
115 | static int hmc6352_probe(struct i2c_client *client, | |
116 | const struct i2c_device_id *id) | |
117 | { | |
118 | int res; | |
119 | ||
120 | res = sysfs_create_group(&client->dev.kobj, &m_compass_gr); | |
121 | if (res) { | |
122 | dev_err(&client->dev, "device_create_file failed\n"); | |
123 | return res; | |
124 | } | |
125 | dev_info(&client->dev, "%s HMC6352 compass chip found\n", | |
126 | client->name); | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static int hmc6352_remove(struct i2c_client *client) | |
131 | { | |
132 | sysfs_remove_group(&client->dev.kobj, &m_compass_gr); | |
133 | return 0; | |
134 | } | |
135 | ||
136 | static struct i2c_device_id hmc6352_id[] = { | |
137 | { "hmc6352", 0 }, | |
138 | { } | |
139 | }; | |
140 | ||
141 | MODULE_DEVICE_TABLE(i2c, hmc6352_id); | |
142 | ||
143 | static struct i2c_driver hmc6352_driver = { | |
144 | .driver = { | |
145 | .name = "hmc6352", | |
146 | }, | |
147 | .probe = hmc6352_probe, | |
148 | .remove = hmc6352_remove, | |
149 | .id_table = hmc6352_id, | |
150 | }; | |
151 | ||
a64fe2ed | 152 | module_i2c_driver(hmc6352_driver); |
cfa3b24c KT |
153 | |
154 | MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com"); | |
155 | MODULE_DESCRIPTION("hmc6352 Compass Driver"); | |
156 | MODULE_LICENSE("GPL v2"); |