diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2014-11-21 13:45:49 -0500 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2014-12-12 07:28:20 -0500 |
commit | 57e73a423b1e85f9b1b0f58e10d38ec00d0c8489 (patch) | |
tree | 731b7f70572aac7f73f23e21e65b1e50f7df3636 /drivers/iio | |
parent | 286f74c2533ac44419819bb3c885ab9f6291d2c3 (diff) |
iio: ak8975: add ak09911 and ak09912 support
Add 2 new definition entries to support ak0991x compass.
Add a more advanced function to check we are dealing with the
expected device.
Remove standalone driver for ak09911.
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Tested-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r-- | drivers/iio/magnetometer/Kconfig | 15 | ||||
-rw-r--r-- | drivers/iio/magnetometer/Makefile | 1 | ||||
-rw-r--r-- | drivers/iio/magnetometer/ak09911.c | 326 | ||||
-rw-r--r-- | drivers/iio/magnetometer/ak8975.c | 196 |
4 files changed, 185 insertions, 353 deletions
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig index b2dba9e506ab..4c7a4c52dd06 100644 --- a/drivers/iio/magnetometer/Kconfig +++ b/drivers/iio/magnetometer/Kconfig | |||
@@ -6,26 +6,21 @@ | |||
6 | menu "Magnetometer sensors" | 6 | menu "Magnetometer sensors" |
7 | 7 | ||
8 | config AK8975 | 8 | config AK8975 |
9 | tristate "Asahi Kasei AK8975 3-Axis Magnetometer" | 9 | tristate "Asahi Kasei AK 3-Axis Magnetometer" |
10 | depends on I2C | 10 | depends on I2C |
11 | depends on GPIOLIB | 11 | depends on GPIOLIB |
12 | help | 12 | help |
13 | Say yes here to build support for Asahi Kasei AK8975 3-Axis | 13 | Say yes here to build support for Asahi Kasei AK8975, AK8963, |
14 | Magnetometer. This driver can also support AK8963, if i2c | 14 | AK09911 or AK09912 3-Axis Magnetometer. |
15 | device name is identified as ak8963. | ||
16 | 15 | ||
17 | To compile this driver as a module, choose M here: the module | 16 | To compile this driver as a module, choose M here: the module |
18 | will be called ak8975. | 17 | will be called ak8975. |
19 | 18 | ||
20 | config AK09911 | 19 | config AK09911 |
21 | tristate "Asahi Kasei AK09911 3-axis Compass" | 20 | tristate "Asahi Kasei AK09911 3-axis Compass" |
22 | depends on I2C | 21 | select AK8975 |
23 | help | 22 | help |
24 | Say yes here to build support for Asahi Kasei AK09911 3-Axis | 23 | Deprecated: AK09911 is now supported by AK8975 driver. |
25 | Magnetometer. | ||
26 | |||
27 | To compile this driver as a module, choose M here: the module | ||
28 | will be called ak09911. | ||
29 | 24 | ||
30 | config MAG3110 | 25 | config MAG3110 |
31 | tristate "Freescale MAG3110 3-Axis Magnetometer" | 26 | tristate "Freescale MAG3110 3-Axis Magnetometer" |
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile index b91315e0b826..0f5d3c985799 100644 --- a/drivers/iio/magnetometer/Makefile +++ b/drivers/iio/magnetometer/Makefile | |||
@@ -3,7 +3,6 @@ | |||
3 | # | 3 | # |
4 | 4 | ||
5 | # When adding new entries keep the list in alphabetical order | 5 | # When adding new entries keep the list in alphabetical order |
6 | obj-$(CONFIG_AK09911) += ak09911.o | ||
7 | obj-$(CONFIG_AK8975) += ak8975.o | 6 | obj-$(CONFIG_AK8975) += ak8975.o |
8 | obj-$(CONFIG_MAG3110) += mag3110.o | 7 | obj-$(CONFIG_MAG3110) += mag3110.o |
9 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o | 8 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o |
diff --git a/drivers/iio/magnetometer/ak09911.c b/drivers/iio/magnetometer/ak09911.c deleted file mode 100644 index b2bc942ff6b8..000000000000 --- a/drivers/iio/magnetometer/ak09911.c +++ /dev/null | |||
@@ -1,326 +0,0 @@ | |||
1 | /* | ||
2 | * AK09911 3-axis compass driver | ||
3 | * Copyright (c) 2014, Intel Corporation. | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify it | ||
6 | * under the terms and conditions of the GNU General Public License, | ||
7 | * version 2, as published by the Free Software Foundation. | ||
8 | * | ||
9 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
12 | * more details. | ||
13 | */ | ||
14 | |||
15 | #include <linux/kernel.h> | ||
16 | #include <linux/module.h> | ||
17 | #include <linux/init.h> | ||
18 | #include <linux/types.h> | ||
19 | #include <linux/slab.h> | ||
20 | #include <linux/delay.h> | ||
21 | #include <linux/i2c.h> | ||
22 | #include <linux/acpi.h> | ||
23 | #include <linux/iio/iio.h> | ||
24 | |||
25 | #define AK09911_REG_WIA1 0x00 | ||
26 | #define AK09911_REG_WIA2 0x01 | ||
27 | #define AK09911_WIA1_VALUE 0x48 | ||
28 | #define AK09911_WIA2_VALUE 0x05 | ||
29 | |||
30 | #define AK09911_REG_ST1 0x10 | ||
31 | #define AK09911_REG_HXL 0x11 | ||
32 | #define AK09911_REG_HXH 0x12 | ||
33 | #define AK09911_REG_HYL 0x13 | ||
34 | #define AK09911_REG_HYH 0x14 | ||
35 | #define AK09911_REG_HZL 0x15 | ||
36 | #define AK09911_REG_HZH 0x16 | ||
37 | |||
38 | #define AK09911_REG_ASAX 0x60 | ||
39 | #define AK09911_REG_ASAY 0x61 | ||
40 | #define AK09911_REG_ASAZ 0x62 | ||
41 | |||
42 | #define AK09911_REG_CNTL1 0x30 | ||
43 | #define AK09911_REG_CNTL2 0x31 | ||
44 | #define AK09911_REG_CNTL3 0x32 | ||
45 | |||
46 | #define AK09911_MODE_SNG_MEASURE 0x01 | ||
47 | #define AK09911_MODE_SELF_TEST 0x10 | ||
48 | #define AK09911_MODE_FUSE_ACCESS 0x1F | ||
49 | #define AK09911_MODE_POWERDOWN 0x00 | ||
50 | #define AK09911_RESET_DATA 0x01 | ||
51 | |||
52 | #define AK09911_REG_CNTL1 0x30 | ||
53 | #define AK09911_REG_CNTL2 0x31 | ||
54 | #define AK09911_REG_CNTL3 0x32 | ||
55 | |||
56 | #define AK09911_RAW_TO_GAUSS(asa) ((((asa) + 128) * 6000) / 256) | ||
57 | |||
58 | #define AK09911_MAX_CONVERSION_TIMEOUT_MS 500 | ||
59 | #define AK09911_CONVERSION_DONE_POLL_TIME_MS 10 | ||
60 | |||
61 | struct ak09911_data { | ||
62 | struct i2c_client *client; | ||
63 | struct mutex lock; | ||
64 | u8 asa[3]; | ||
65 | long raw_to_gauss[3]; | ||
66 | }; | ||
67 | |||
68 | static const int ak09911_index_to_reg[] = { | ||
69 | AK09911_REG_HXL, AK09911_REG_HYL, AK09911_REG_HZL, | ||
70 | }; | ||
71 | |||
72 | static int ak09911_set_mode(struct i2c_client *client, u8 mode) | ||
73 | { | ||
74 | int ret; | ||
75 | |||
76 | switch (mode) { | ||
77 | case AK09911_MODE_SNG_MEASURE: | ||
78 | case AK09911_MODE_SELF_TEST: | ||
79 | case AK09911_MODE_FUSE_ACCESS: | ||
80 | case AK09911_MODE_POWERDOWN: | ||
81 | ret = i2c_smbus_write_byte_data(client, | ||
82 | AK09911_REG_CNTL2, mode); | ||
83 | if (ret < 0) { | ||
84 | dev_err(&client->dev, "set_mode error\n"); | ||
85 | return ret; | ||
86 | } | ||
87 | /* After mode change wait atleast 100us */ | ||
88 | usleep_range(100, 500); | ||
89 | break; | ||
90 | default: | ||
91 | dev_err(&client->dev, | ||
92 | "%s: Unknown mode(%d).", __func__, mode); | ||
93 | return -EINVAL; | ||
94 | } | ||
95 | |||
96 | return ret; | ||
97 | } | ||
98 | |||
99 | /* Get Sensitivity Adjustment value */ | ||
100 | static int ak09911_get_asa(struct i2c_client *client) | ||
101 | { | ||
102 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
103 | struct ak09911_data *data = iio_priv(indio_dev); | ||
104 | int ret; | ||
105 | |||
106 | ret = ak09911_set_mode(client, AK09911_MODE_FUSE_ACCESS); | ||
107 | if (ret < 0) | ||
108 | return ret; | ||
109 | |||
110 | /* Get asa data and store in the device data. */ | ||
111 | ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_ASAX, | ||
112 | 3, data->asa); | ||
113 | if (ret < 0) { | ||
114 | dev_err(&client->dev, "Not able to read asa data\n"); | ||
115 | return ret; | ||
116 | } | ||
117 | |||
118 | ret = ak09911_set_mode(client, AK09911_MODE_POWERDOWN); | ||
119 | if (ret < 0) | ||
120 | return ret; | ||
121 | |||
122 | data->raw_to_gauss[0] = AK09911_RAW_TO_GAUSS(data->asa[0]); | ||
123 | data->raw_to_gauss[1] = AK09911_RAW_TO_GAUSS(data->asa[1]); | ||
124 | data->raw_to_gauss[2] = AK09911_RAW_TO_GAUSS(data->asa[2]); | ||
125 | |||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | static int ak09911_verify_chip_id(struct i2c_client *client) | ||
130 | { | ||
131 | u8 wia_val[2]; | ||
132 | int ret; | ||
133 | |||
134 | ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_WIA1, | ||
135 | 2, wia_val); | ||
136 | if (ret < 0) { | ||
137 | dev_err(&client->dev, "Error reading WIA\n"); | ||
138 | return ret; | ||
139 | } | ||
140 | |||
141 | dev_dbg(&client->dev, "WIA %02x %02x\n", wia_val[0], wia_val[1]); | ||
142 | |||
143 | if (wia_val[0] != AK09911_WIA1_VALUE || | ||
144 | wia_val[1] != AK09911_WIA2_VALUE) { | ||
145 | dev_err(&client->dev, "Device ak09911 not found\n"); | ||
146 | return -ENODEV; | ||
147 | } | ||
148 | |||
149 | return 0; | ||
150 | } | ||
151 | |||
152 | static int wait_conversion_complete_polled(struct ak09911_data *data) | ||
153 | { | ||
154 | struct i2c_client *client = data->client; | ||
155 | u8 read_status; | ||
156 | u32 timeout_ms = AK09911_MAX_CONVERSION_TIMEOUT_MS; | ||
157 | int ret; | ||
158 | |||
159 | /* Wait for the conversion to complete. */ | ||
160 | while (timeout_ms) { | ||
161 | msleep_interruptible(AK09911_CONVERSION_DONE_POLL_TIME_MS); | ||
162 | ret = i2c_smbus_read_byte_data(client, AK09911_REG_ST1); | ||
163 | if (ret < 0) { | ||
164 | dev_err(&client->dev, "Error in reading ST1\n"); | ||
165 | return ret; | ||
166 | } | ||
167 | read_status = ret & 0x01; | ||
168 | if (read_status) | ||
169 | break; | ||
170 | timeout_ms -= AK09911_CONVERSION_DONE_POLL_TIME_MS; | ||
171 | } | ||
172 | if (!timeout_ms) { | ||
173 | dev_err(&client->dev, "Conversion timeout happened\n"); | ||
174 | return -EIO; | ||
175 | } | ||
176 | |||
177 | return read_status; | ||
178 | } | ||
179 | |||
180 | static int ak09911_read_axis(struct iio_dev *indio_dev, int index, int *val) | ||
181 | { | ||
182 | struct ak09911_data *data = iio_priv(indio_dev); | ||
183 | struct i2c_client *client = data->client; | ||
184 | int ret; | ||
185 | |||
186 | mutex_lock(&data->lock); | ||
187 | |||
188 | ret = ak09911_set_mode(client, AK09911_MODE_SNG_MEASURE); | ||
189 | if (ret < 0) | ||
190 | goto fn_exit; | ||
191 | |||
192 | ret = wait_conversion_complete_polled(data); | ||
193 | if (ret < 0) | ||
194 | goto fn_exit; | ||
195 | |||
196 | /* Read data */ | ||
197 | ret = i2c_smbus_read_word_data(client, ak09911_index_to_reg[index]); | ||
198 | if (ret < 0) { | ||
199 | dev_err(&client->dev, "Read axis data fails\n"); | ||
200 | goto fn_exit; | ||
201 | } | ||
202 | |||
203 | mutex_unlock(&data->lock); | ||
204 | |||
205 | /* Clamp to valid range. */ | ||
206 | *val = sign_extend32(clamp_t(s16, ret, -8192, 8191), 13); | ||
207 | |||
208 | return IIO_VAL_INT; | ||
209 | |||
210 | fn_exit: | ||
211 | mutex_unlock(&data->lock); | ||
212 | |||
213 | return ret; | ||
214 | } | ||
215 | |||
216 | static int ak09911_read_raw(struct iio_dev *indio_dev, | ||
217 | struct iio_chan_spec const *chan, | ||
218 | int *val, int *val2, | ||
219 | long mask) | ||
220 | { | ||
221 | struct ak09911_data *data = iio_priv(indio_dev); | ||
222 | |||
223 | switch (mask) { | ||
224 | case IIO_CHAN_INFO_RAW: | ||
225 | return ak09911_read_axis(indio_dev, chan->address, val); | ||
226 | case IIO_CHAN_INFO_SCALE: | ||
227 | *val = 0; | ||
228 | *val2 = data->raw_to_gauss[chan->address]; | ||
229 | return IIO_VAL_INT_PLUS_MICRO; | ||
230 | } | ||
231 | |||
232 | return -EINVAL; | ||
233 | } | ||
234 | |||
235 | #define AK09911_CHANNEL(axis, index) \ | ||
236 | { \ | ||
237 | .type = IIO_MAGN, \ | ||
238 | .modified = 1, \ | ||
239 | .channel2 = IIO_MOD_##axis, \ | ||
240 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ | ||
241 | BIT(IIO_CHAN_INFO_SCALE), \ | ||
242 | .address = index, \ | ||
243 | } | ||
244 | |||
245 | static const struct iio_chan_spec ak09911_channels[] = { | ||
246 | AK09911_CHANNEL(X, 0), AK09911_CHANNEL(Y, 1), AK09911_CHANNEL(Z, 2), | ||
247 | }; | ||
248 | |||
249 | static const struct iio_info ak09911_info = { | ||
250 | .read_raw = &ak09911_read_raw, | ||
251 | .driver_module = THIS_MODULE, | ||
252 | }; | ||
253 | |||
254 | static const struct acpi_device_id ak_acpi_match[] = { | ||
255 | {"AK009911", 0}, | ||
256 | { }, | ||
257 | }; | ||
258 | MODULE_DEVICE_TABLE(acpi, ak_acpi_match); | ||
259 | |||
260 | static int ak09911_probe(struct i2c_client *client, | ||
261 | const struct i2c_device_id *id) | ||
262 | { | ||
263 | struct iio_dev *indio_dev; | ||
264 | struct ak09911_data *data; | ||
265 | const char *name; | ||
266 | int ret; | ||
267 | |||
268 | ret = ak09911_verify_chip_id(client); | ||
269 | if (ret) { | ||
270 | dev_err(&client->dev, "AK00911 not detected\n"); | ||
271 | return -ENODEV; | ||
272 | } | ||
273 | |||
274 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | ||
275 | if (indio_dev == NULL) | ||
276 | return -ENOMEM; | ||
277 | |||
278 | data = iio_priv(indio_dev); | ||
279 | i2c_set_clientdata(client, indio_dev); | ||
280 | |||
281 | data->client = client; | ||
282 | mutex_init(&data->lock); | ||
283 | |||
284 | ret = ak09911_get_asa(client); | ||
285 | if (ret) | ||
286 | return ret; | ||
287 | |||
288 | if (id) | ||
289 | name = id->name; | ||
290 | else if (ACPI_HANDLE(&client->dev)) | ||
291 | name = dev_name(&client->dev); | ||
292 | else | ||
293 | return -ENODEV; | ||
294 | |||
295 | dev_dbg(&client->dev, "Asahi compass chip %s\n", name); | ||
296 | |||
297 | indio_dev->dev.parent = &client->dev; | ||
298 | indio_dev->channels = ak09911_channels; | ||
299 | indio_dev->num_channels = ARRAY_SIZE(ak09911_channels); | ||
300 | indio_dev->info = &ak09911_info; | ||
301 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
302 | indio_dev->name = name; | ||
303 | |||
304 | return devm_iio_device_register(&client->dev, indio_dev); | ||
305 | } | ||
306 | |||
307 | static const struct i2c_device_id ak09911_id[] = { | ||
308 | {"ak09911", 0}, | ||
309 | {} | ||
310 | }; | ||
311 | |||
312 | MODULE_DEVICE_TABLE(i2c, ak09911_id); | ||
313 | |||
314 | static struct i2c_driver ak09911_driver = { | ||
315 | .driver = { | ||
316 | .name = "ak09911", | ||
317 | .acpi_match_table = ACPI_PTR(ak_acpi_match), | ||
318 | }, | ||
319 | .probe = ak09911_probe, | ||
320 | .id_table = ak09911_id, | ||
321 | }; | ||
322 | module_i2c_driver(ak09911_driver); | ||
323 | |||
324 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); | ||
325 | MODULE_LICENSE("GPL v2"); | ||
326 | MODULE_DESCRIPTION("AK09911 Compass driver"); | ||
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c index 0f86a8e72034..0d10a4baceb6 100644 --- a/drivers/iio/magnetometer/ak8975.c +++ b/drivers/iio/magnetometer/ak8975.c | |||
@@ -81,6 +81,58 @@ | |||
81 | #define AK8975_MAX_REGS AK8975_REG_ASAZ | 81 | #define AK8975_MAX_REGS AK8975_REG_ASAZ |
82 | 82 | ||
83 | /* | 83 | /* |
84 | * AK09912 Register definitions | ||
85 | */ | ||
86 | #define AK09912_REG_WIA1 0x00 | ||
87 | #define AK09912_REG_WIA2 0x01 | ||
88 | #define AK09912_DEVICE_ID 0x04 | ||
89 | #define AK09911_DEVICE_ID 0x05 | ||
90 | |||
91 | #define AK09911_REG_INFO1 0x02 | ||
92 | #define AK09911_REG_INFO2 0x03 | ||
93 | |||
94 | #define AK09912_REG_ST1 0x10 | ||
95 | |||
96 | #define AK09912_REG_ST1_DRDY_SHIFT 0 | ||
97 | #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT) | ||
98 | |||
99 | #define AK09912_REG_HXL 0x11 | ||
100 | #define AK09912_REG_HXH 0x12 | ||
101 | #define AK09912_REG_HYL 0x13 | ||
102 | #define AK09912_REG_HYH 0x14 | ||
103 | #define AK09912_REG_HZL 0x15 | ||
104 | #define AK09912_REG_HZH 0x16 | ||
105 | #define AK09912_REG_TMPS 0x17 | ||
106 | |||
107 | #define AK09912_REG_ST2 0x18 | ||
108 | #define AK09912_REG_ST2_HOFL_SHIFT 3 | ||
109 | #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT) | ||
110 | |||
111 | #define AK09912_REG_CNTL1 0x30 | ||
112 | |||
113 | #define AK09912_REG_CNTL2 0x31 | ||
114 | #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00 | ||
115 | #define AK09912_REG_CNTL_MODE_ONCE 0x01 | ||
116 | #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10 | ||
117 | #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F | ||
118 | #define AK09912_REG_CNTL2_MODE_SHIFT 0 | ||
119 | #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT) | ||
120 | |||
121 | #define AK09912_REG_CNTL3 0x32 | ||
122 | |||
123 | #define AK09912_REG_TS1 0x33 | ||
124 | #define AK09912_REG_TS2 0x34 | ||
125 | #define AK09912_REG_TS3 0x35 | ||
126 | #define AK09912_REG_I2CDIS 0x36 | ||
127 | #define AK09912_REG_TS4 0x37 | ||
128 | |||
129 | #define AK09912_REG_ASAX 0x60 | ||
130 | #define AK09912_REG_ASAY 0x61 | ||
131 | #define AK09912_REG_ASAZ 0x62 | ||
132 | |||
133 | #define AK09912_MAX_REGS AK09912_REG_ASAZ | ||
134 | |||
135 | /* | ||
84 | * Miscellaneous values. | 136 | * Miscellaneous values. |
85 | */ | 137 | */ |
86 | #define AK8975_MAX_CONVERSION_TIMEOUT 500 | 138 | #define AK8975_MAX_CONVERSION_TIMEOUT 500 |
@@ -130,22 +182,38 @@ static long ak8975_raw_to_gauss(u16 data) | |||
130 | } | 182 | } |
131 | 183 | ||
132 | /* | 184 | /* |
133 | * For AK8963, same calculation, but the device is less sensitive: | 185 | * For AK8963 and AK09911, same calculation, but the device is less sensitive: |
134 | * | 186 | * |
135 | * H is in the range of +-8190. The magnetometer has a range of | 187 | * H is in the range of +-8190. The magnetometer has a range of |
136 | * +-4912uT. To go from the raw value to uT is: | 188 | * +-4912uT. To go from the raw value to uT is: |
137 | * | 189 | * |
138 | * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10. | 190 | * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10. |
139 | */ | 191 | */ |
140 | static long ak8963_raw_to_gauss(u16 data) | 192 | |
193 | static long ak8963_09911_raw_to_gauss(u16 data) | ||
141 | { | 194 | { |
142 | return (((long)data + 128) * 6000) / 256; | 195 | return (((long)data + 128) * 6000) / 256; |
143 | } | 196 | } |
144 | 197 | ||
198 | /* | ||
199 | * For AK09912, same calculation, except the device is more sensitive: | ||
200 | * | ||
201 | * H is in the range of -32752 to 32752. The magnetometer has a range of | ||
202 | * +-4912uT. To go from the raw value to uT is: | ||
203 | * | ||
204 | * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10. | ||
205 | */ | ||
206 | static long ak09912_raw_to_gauss(u16 data) | ||
207 | { | ||
208 | return (((long)data + 128) * 1500) / 256; | ||
209 | } | ||
210 | |||
145 | /* Compatible Asahi Kasei Compass parts */ | 211 | /* Compatible Asahi Kasei Compass parts */ |
146 | enum asahi_compass_chipset { | 212 | enum asahi_compass_chipset { |
147 | AK8975, | 213 | AK8975, |
148 | AK8963, | 214 | AK8963, |
215 | AK09911, | ||
216 | AK09912, | ||
149 | AK_MAX_TYPE | 217 | AK_MAX_TYPE |
150 | }; | 218 | }; |
151 | 219 | ||
@@ -212,7 +280,7 @@ static struct ak_def ak_def_array[AK_MAX_TYPE] = { | |||
212 | }, | 280 | }, |
213 | { | 281 | { |
214 | .type = AK8963, | 282 | .type = AK8963, |
215 | .raw_to_gauss = ak8963_raw_to_gauss, | 283 | .raw_to_gauss = ak8963_09911_raw_to_gauss, |
216 | .range = 8190, | 284 | .range = 8190, |
217 | .ctrl_regs = { | 285 | .ctrl_regs = { |
218 | AK8975_REG_ST1, | 286 | AK8975_REG_ST1, |
@@ -235,6 +303,56 @@ static struct ak_def ak_def_array[AK_MAX_TYPE] = { | |||
235 | AK8975_REG_HYL, | 303 | AK8975_REG_HYL, |
236 | AK8975_REG_HZL}, | 304 | AK8975_REG_HZL}, |
237 | }, | 305 | }, |
306 | { | ||
307 | .type = AK09911, | ||
308 | .raw_to_gauss = ak8963_09911_raw_to_gauss, | ||
309 | .range = 8192, | ||
310 | .ctrl_regs = { | ||
311 | AK09912_REG_ST1, | ||
312 | AK09912_REG_ST2, | ||
313 | AK09912_REG_CNTL2, | ||
314 | AK09912_REG_ASAX, | ||
315 | AK09912_MAX_REGS}, | ||
316 | .ctrl_masks = { | ||
317 | AK09912_REG_ST1_DRDY_MASK, | ||
318 | AK09912_REG_ST2_HOFL_MASK, | ||
319 | 0, | ||
320 | AK09912_REG_CNTL2_MODE_MASK}, | ||
321 | .ctrl_modes = { | ||
322 | AK09912_REG_CNTL_MODE_POWER_DOWN, | ||
323 | AK09912_REG_CNTL_MODE_ONCE, | ||
324 | AK09912_REG_CNTL_MODE_SELF_TEST, | ||
325 | AK09912_REG_CNTL_MODE_FUSE_ROM}, | ||
326 | .data_regs = { | ||
327 | AK09912_REG_HXL, | ||
328 | AK09912_REG_HYL, | ||
329 | AK09912_REG_HZL}, | ||
330 | }, | ||
331 | { | ||
332 | .type = AK09912, | ||
333 | .raw_to_gauss = ak09912_raw_to_gauss, | ||
334 | .range = 32752, | ||
335 | .ctrl_regs = { | ||
336 | AK09912_REG_ST1, | ||
337 | AK09912_REG_ST2, | ||
338 | AK09912_REG_CNTL2, | ||
339 | AK09912_REG_ASAX, | ||
340 | AK09912_MAX_REGS}, | ||
341 | .ctrl_masks = { | ||
342 | AK09912_REG_ST1_DRDY_MASK, | ||
343 | AK09912_REG_ST2_HOFL_MASK, | ||
344 | 0, | ||
345 | AK09912_REG_CNTL2_MODE_MASK}, | ||
346 | .ctrl_modes = { | ||
347 | AK09912_REG_CNTL_MODE_POWER_DOWN, | ||
348 | AK09912_REG_CNTL_MODE_ONCE, | ||
349 | AK09912_REG_CNTL_MODE_SELF_TEST, | ||
350 | AK09912_REG_CNTL_MODE_FUSE_ROM}, | ||
351 | .data_regs = { | ||
352 | AK09912_REG_HXL, | ||
353 | AK09912_REG_HYL, | ||
354 | AK09912_REG_HZL}, | ||
355 | } | ||
238 | }; | 356 | }; |
239 | 357 | ||
240 | /* | 358 | /* |
@@ -255,6 +373,52 @@ struct ak8975_data { | |||
255 | }; | 373 | }; |
256 | 374 | ||
257 | /* | 375 | /* |
376 | * Return 0 if the i2c device is the one we expect. | ||
377 | * return a negative error number otherwise | ||
378 | */ | ||
379 | static int ak8975_who_i_am(struct i2c_client *client, | ||
380 | enum asahi_compass_chipset type) | ||
381 | { | ||
382 | u8 wia_val[2]; | ||
383 | int ret; | ||
384 | |||
385 | /* | ||
386 | * Signature for each device: | ||
387 | * Device | WIA1 | WIA2 | ||
388 | * AK09912 | DEVICE_ID | AK09912_DEVICE_ID | ||
389 | * AK09911 | DEVICE_ID | AK09911_DEVICE_ID | ||
390 | * AK8975 | DEVICE_ID | NA | ||
391 | * AK8963 | DEVICE_ID | NA | ||
392 | */ | ||
393 | ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1, | ||
394 | 2, wia_val); | ||
395 | if (ret < 0) { | ||
396 | dev_err(&client->dev, "Error reading WIA\n"); | ||
397 | return ret; | ||
398 | } | ||
399 | |||
400 | if (wia_val[0] != AK8975_DEVICE_ID) | ||
401 | return -ENODEV; | ||
402 | |||
403 | switch (type) { | ||
404 | case AK8975: | ||
405 | case AK8963: | ||
406 | return 0; | ||
407 | case AK09911: | ||
408 | if (wia_val[1] == AK09911_DEVICE_ID) | ||
409 | return 0; | ||
410 | break; | ||
411 | case AK09912: | ||
412 | if (wia_val[1] == AK09912_DEVICE_ID) | ||
413 | return 0; | ||
414 | break; | ||
415 | default: | ||
416 | dev_err(&client->dev, "Type %d unknown\n", type); | ||
417 | } | ||
418 | return -ENODEV; | ||
419 | } | ||
420 | |||
421 | /* | ||
258 | * Helper function to write to CNTL register. | 422 | * Helper function to write to CNTL register. |
259 | */ | 423 | */ |
260 | static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) | 424 | static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) |
@@ -329,21 +493,8 @@ static int ak8975_setup(struct i2c_client *client) | |||
329 | { | 493 | { |
330 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | 494 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
331 | struct ak8975_data *data = iio_priv(indio_dev); | 495 | struct ak8975_data *data = iio_priv(indio_dev); |
332 | u8 device_id; | ||
333 | int ret; | 496 | int ret; |
334 | 497 | ||
335 | /* Confirm that the device we're talking to is really an AK8975. */ | ||
336 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA); | ||
337 | if (ret < 0) { | ||
338 | dev_err(&client->dev, "Error reading WIA\n"); | ||
339 | return ret; | ||
340 | } | ||
341 | device_id = ret; | ||
342 | if (device_id != AK8975_DEVICE_ID) { | ||
343 | dev_err(&client->dev, "Device ak8975 not found\n"); | ||
344 | return -ENODEV; | ||
345 | } | ||
346 | |||
347 | /* Write the fused rom access mode. */ | 498 | /* Write the fused rom access mode. */ |
348 | ret = ak8975_set_mode(data, FUSE_ROM); | 499 | ret = ak8975_set_mode(data, FUSE_ROM); |
349 | if (ret < 0) { | 500 | if (ret < 0) { |
@@ -554,6 +705,8 @@ static const struct acpi_device_id ak_acpi_match[] = { | |||
554 | {"AK8975", AK8975}, | 705 | {"AK8975", AK8975}, |
555 | {"AK8963", AK8963}, | 706 | {"AK8963", AK8963}, |
556 | {"INVN6500", AK8963}, | 707 | {"INVN6500", AK8963}, |
708 | {"AK09911", AK09911}, | ||
709 | {"AK09912", AK09912}, | ||
557 | { }, | 710 | { }, |
558 | }; | 711 | }; |
559 | MODULE_DEVICE_TABLE(acpi, ak_acpi_match); | 712 | MODULE_DEVICE_TABLE(acpi, ak_acpi_match); |
@@ -633,6 +786,11 @@ static int ak8975_probe(struct i2c_client *client, | |||
633 | } | 786 | } |
634 | 787 | ||
635 | data->def = &ak_def_array[chipset]; | 788 | data->def = &ak_def_array[chipset]; |
789 | err = ak8975_who_i_am(client, data->def->type); | ||
790 | if (err < 0) { | ||
791 | dev_err(&client->dev, "Unexpected device\n"); | ||
792 | return err; | ||
793 | } | ||
636 | dev_dbg(&client->dev, "Asahi compass chip %s\n", name); | 794 | dev_dbg(&client->dev, "Asahi compass chip %s\n", name); |
637 | 795 | ||
638 | /* Perform some basic start-of-day setup of the device. */ | 796 | /* Perform some basic start-of-day setup of the device. */ |
@@ -655,6 +813,8 @@ static int ak8975_probe(struct i2c_client *client, | |||
655 | static const struct i2c_device_id ak8975_id[] = { | 813 | static const struct i2c_device_id ak8975_id[] = { |
656 | {"ak8975", AK8975}, | 814 | {"ak8975", AK8975}, |
657 | {"ak8963", AK8963}, | 815 | {"ak8963", AK8963}, |
816 | {"ak09911", AK09911}, | ||
817 | {"ak09912", AK09912}, | ||
658 | {} | 818 | {} |
659 | }; | 819 | }; |
660 | 820 | ||
@@ -665,6 +825,10 @@ static const struct of_device_id ak8975_of_match[] = { | |||
665 | { .compatible = "ak8975", }, | 825 | { .compatible = "ak8975", }, |
666 | { .compatible = "asahi-kasei,ak8963", }, | 826 | { .compatible = "asahi-kasei,ak8963", }, |
667 | { .compatible = "ak8963", }, | 827 | { .compatible = "ak8963", }, |
828 | { .compatible = "asahi-kasei,ak09911", }, | ||
829 | { .compatible = "ak09911", }, | ||
830 | { .compatible = "asahi-kasei,ak09912", }, | ||
831 | { .compatible = "ak09912", }, | ||
668 | {} | 832 | {} |
669 | }; | 833 | }; |
670 | MODULE_DEVICE_TABLE(of, ak8975_of_match); | 834 | MODULE_DEVICE_TABLE(of, ak8975_of_match); |