]>
Commit | Line | Data |
---|---|---|
3285aae1 AC |
1 | /* |
2 | * A sensor driver for the magnetometer AK8975. | |
3 | * | |
4 | * Magnetic compass sensor driver for monitoring magnetic flux information. | |
5 | * | |
6 | * Copyright (c) 2010, NVIDIA Corporation. | |
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; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/i2c.h> | |
94a6d5cf | 27 | #include <linux/interrupt.h> |
3285aae1 AC |
28 | #include <linux/err.h> |
29 | #include <linux/mutex.h> | |
30 | #include <linux/delay.h> | |
94a6d5cf | 31 | #include <linux/bitops.h> |
3285aae1 | 32 | #include <linux/gpio.h> |
f4b7f751 | 33 | #include <linux/of_gpio.h> |
d913971e | 34 | #include <linux/acpi.h> |
3285aae1 | 35 | |
06458e27 JC |
36 | #include <linux/iio/iio.h> |
37 | #include <linux/iio/sysfs.h> | |
3285aae1 AC |
38 | /* |
39 | * Register definitions, as well as various shifts and masks to get at the | |
40 | * individual fields of the registers. | |
41 | */ | |
42 | #define AK8975_REG_WIA 0x00 | |
43 | #define AK8975_DEVICE_ID 0x48 | |
44 | ||
45 | #define AK8975_REG_INFO 0x01 | |
46 | ||
47 | #define AK8975_REG_ST1 0x02 | |
48 | #define AK8975_REG_ST1_DRDY_SHIFT 0 | |
49 | #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT) | |
50 | ||
51 | #define AK8975_REG_HXL 0x03 | |
52 | #define AK8975_REG_HXH 0x04 | |
53 | #define AK8975_REG_HYL 0x05 | |
54 | #define AK8975_REG_HYH 0x06 | |
55 | #define AK8975_REG_HZL 0x07 | |
56 | #define AK8975_REG_HZH 0x08 | |
57 | #define AK8975_REG_ST2 0x09 | |
58 | #define AK8975_REG_ST2_DERR_SHIFT 2 | |
59 | #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT) | |
60 | ||
61 | #define AK8975_REG_ST2_HOFL_SHIFT 3 | |
62 | #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT) | |
63 | ||
64 | #define AK8975_REG_CNTL 0x0A | |
65 | #define AK8975_REG_CNTL_MODE_SHIFT 0 | |
66 | #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT) | |
71222bf5 GG |
67 | #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00 |
68 | #define AK8975_REG_CNTL_MODE_ONCE 0x01 | |
69 | #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08 | |
70 | #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F | |
3285aae1 AC |
71 | |
72 | #define AK8975_REG_RSVC 0x0B | |
73 | #define AK8975_REG_ASTC 0x0C | |
74 | #define AK8975_REG_TS1 0x0D | |
75 | #define AK8975_REG_TS2 0x0E | |
76 | #define AK8975_REG_I2CDIS 0x0F | |
77 | #define AK8975_REG_ASAX 0x10 | |
78 | #define AK8975_REG_ASAY 0x11 | |
79 | #define AK8975_REG_ASAZ 0x12 | |
80 | ||
81 | #define AK8975_MAX_REGS AK8975_REG_ASAZ | |
82 | ||
57e73a42 GG |
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 | ||
3285aae1 AC |
135 | /* |
136 | * Miscellaneous values. | |
137 | */ | |
138 | #define AK8975_MAX_CONVERSION_TIMEOUT 500 | |
139 | #define AK8975_CONVERSION_DONE_POLL_TIME 10 | |
94a6d5cf | 140 | #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) |
286f74c2 GG |
141 | |
142 | /* | |
143 | * Precalculate scale factor (in Gauss units) for each axis and | |
144 | * store in the device data. | |
145 | * | |
146 | * This scale factor is axis-dependent, and is derived from 3 calibration | |
147 | * factors ASA(x), ASA(y), and ASA(z). | |
148 | * | |
149 | * These ASA values are read from the sensor device at start of day, and | |
150 | * cached in the device context struct. | |
151 | * | |
152 | * Adjusting the flux value with the sensitivity adjustment value should be | |
153 | * done via the following formula: | |
154 | * | |
155 | * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) | |
156 | * where H is the raw value, ASA is the sensitivity adjustment, and Hadj | |
157 | * is the resultant adjusted value. | |
158 | * | |
159 | * We reduce the formula to: | |
160 | * | |
161 | * Hadj = H * (ASA + 128) / 256 | |
162 | * | |
163 | * H is in the range of -4096 to 4095. The magnetometer has a range of | |
164 | * +-1229uT. To go from the raw value to uT is: | |
165 | * | |
166 | * HuT = H * 1229/4096, or roughly, 3/10. | |
167 | * | |
168 | * Since 1uT = 0.01 gauss, our final scale factor becomes: | |
169 | * | |
170 | * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 | |
171 | * Hadj = H * ((ASA + 128) * 0.003) / 256 | |
172 | * | |
173 | * Since ASA doesn't change, we cache the resultant scale factor into the | |
174 | * device context in ak8975_setup(). | |
175 | * | |
176 | * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we | |
177 | * multiply the stored scale value by 1e6. | |
178 | */ | |
179 | static long ak8975_raw_to_gauss(u16 data) | |
180 | { | |
181 | return (((long)data + 128) * 3000) / 256; | |
182 | } | |
183 | ||
184 | /* | |
57e73a42 | 185 | * For AK8963 and AK09911, same calculation, but the device is less sensitive: |
286f74c2 GG |
186 | * |
187 | * H is in the range of +-8190. The magnetometer has a range of | |
188 | * +-4912uT. To go from the raw value to uT is: | |
189 | * | |
190 | * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10. | |
191 | */ | |
57e73a42 GG |
192 | |
193 | static long ak8963_09911_raw_to_gauss(u16 data) | |
286f74c2 GG |
194 | { |
195 | return (((long)data + 128) * 6000) / 256; | |
196 | } | |
6027c077 | 197 | |
57e73a42 GG |
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 | ||
6027c077 SP |
211 | /* Compatible Asahi Kasei Compass parts */ |
212 | enum asahi_compass_chipset { | |
213 | AK8975, | |
214 | AK8963, | |
57e73a42 GG |
215 | AK09911, |
216 | AK09912, | |
286f74c2 GG |
217 | AK_MAX_TYPE |
218 | }; | |
219 | ||
220 | enum ak_ctrl_reg_addr { | |
221 | ST1, | |
222 | ST2, | |
223 | CNTL, | |
224 | ASA_BASE, | |
225 | MAX_REGS, | |
226 | REGS_END, | |
227 | }; | |
228 | ||
229 | enum ak_ctrl_reg_mask { | |
230 | ST1_DRDY, | |
231 | ST2_HOFL, | |
232 | ST2_DERR, | |
233 | CNTL_MODE, | |
234 | MASK_END, | |
235 | }; | |
236 | ||
237 | enum ak_ctrl_mode { | |
238 | POWER_DOWN, | |
239 | MODE_ONCE, | |
240 | SELF_TEST, | |
241 | FUSE_ROM, | |
242 | MODE_END, | |
243 | }; | |
244 | ||
245 | struct ak_def { | |
246 | enum asahi_compass_chipset type; | |
247 | long (*raw_to_gauss)(u16 data); | |
248 | u16 range; | |
249 | u8 ctrl_regs[REGS_END]; | |
250 | u8 ctrl_masks[MASK_END]; | |
251 | u8 ctrl_modes[MODE_END]; | |
252 | u8 data_regs[3]; | |
253 | }; | |
254 | ||
f78c5f96 | 255 | static const struct ak_def ak_def_array[AK_MAX_TYPE] = { |
286f74c2 GG |
256 | { |
257 | .type = AK8975, | |
258 | .raw_to_gauss = ak8975_raw_to_gauss, | |
259 | .range = 4096, | |
260 | .ctrl_regs = { | |
261 | AK8975_REG_ST1, | |
262 | AK8975_REG_ST2, | |
263 | AK8975_REG_CNTL, | |
264 | AK8975_REG_ASAX, | |
265 | AK8975_MAX_REGS}, | |
266 | .ctrl_masks = { | |
267 | AK8975_REG_ST1_DRDY_MASK, | |
268 | AK8975_REG_ST2_HOFL_MASK, | |
269 | AK8975_REG_ST2_DERR_MASK, | |
270 | AK8975_REG_CNTL_MODE_MASK}, | |
271 | .ctrl_modes = { | |
272 | AK8975_REG_CNTL_MODE_POWER_DOWN, | |
273 | AK8975_REG_CNTL_MODE_ONCE, | |
274 | AK8975_REG_CNTL_MODE_SELF_TEST, | |
275 | AK8975_REG_CNTL_MODE_FUSE_ROM}, | |
276 | .data_regs = { | |
277 | AK8975_REG_HXL, | |
278 | AK8975_REG_HYL, | |
279 | AK8975_REG_HZL}, | |
280 | }, | |
281 | { | |
282 | .type = AK8963, | |
57e73a42 | 283 | .raw_to_gauss = ak8963_09911_raw_to_gauss, |
286f74c2 GG |
284 | .range = 8190, |
285 | .ctrl_regs = { | |
286 | AK8975_REG_ST1, | |
287 | AK8975_REG_ST2, | |
288 | AK8975_REG_CNTL, | |
289 | AK8975_REG_ASAX, | |
290 | AK8975_MAX_REGS}, | |
291 | .ctrl_masks = { | |
292 | AK8975_REG_ST1_DRDY_MASK, | |
293 | AK8975_REG_ST2_HOFL_MASK, | |
294 | 0, | |
295 | AK8975_REG_CNTL_MODE_MASK}, | |
296 | .ctrl_modes = { | |
297 | AK8975_REG_CNTL_MODE_POWER_DOWN, | |
298 | AK8975_REG_CNTL_MODE_ONCE, | |
299 | AK8975_REG_CNTL_MODE_SELF_TEST, | |
300 | AK8975_REG_CNTL_MODE_FUSE_ROM}, | |
301 | .data_regs = { | |
302 | AK8975_REG_HXL, | |
303 | AK8975_REG_HYL, | |
304 | AK8975_REG_HZL}, | |
305 | }, | |
57e73a42 GG |
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 | } | |
6027c077 | 356 | }; |
3285aae1 AC |
357 | |
358 | /* | |
359 | * Per-instance context data for the device. | |
360 | */ | |
361 | struct ak8975_data { | |
362 | struct i2c_client *client; | |
f78c5f96 | 363 | const struct ak_def *def; |
3285aae1 AC |
364 | struct attribute_group attrs; |
365 | struct mutex lock; | |
366 | u8 asa[3]; | |
367 | long raw_to_gauss[3]; | |
3285aae1 | 368 | int eoc_gpio; |
94a6d5cf JA |
369 | int eoc_irq; |
370 | wait_queue_head_t data_ready_queue; | |
371 | unsigned long flags; | |
286f74c2 | 372 | u8 cntl_cache; |
694e1b5f JC |
373 | }; |
374 | ||
57e73a42 GG |
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 | ||
3285aae1 | 421 | /* |
286f74c2 | 422 | * Helper function to write to CNTL register. |
3285aae1 | 423 | */ |
286f74c2 | 424 | static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) |
3285aae1 | 425 | { |
694e1b5f JC |
426 | u8 regval; |
427 | int ret; | |
3285aae1 | 428 | |
286f74c2 GG |
429 | regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) | |
430 | data->def->ctrl_modes[mode]; | |
431 | ret = i2c_smbus_write_byte_data(data->client, | |
432 | data->def->ctrl_regs[CNTL], regval); | |
3285aae1 | 433 | if (ret < 0) { |
3285aae1 AC |
434 | return ret; |
435 | } | |
286f74c2 GG |
436 | data->cntl_cache = regval; |
437 | /* After mode change wait atleast 100us */ | |
438 | usleep_range(100, 500); | |
3285aae1 AC |
439 | |
440 | return 0; | |
441 | } | |
442 | ||
94a6d5cf JA |
443 | /* |
444 | * Handle data ready irq | |
445 | */ | |
446 | static irqreturn_t ak8975_irq_handler(int irq, void *data) | |
447 | { | |
448 | struct ak8975_data *ak8975 = data; | |
449 | ||
450 | set_bit(0, &ak8975->flags); | |
451 | wake_up(&ak8975->data_ready_queue); | |
452 | ||
453 | return IRQ_HANDLED; | |
454 | } | |
455 | ||
456 | /* | |
457 | * Install data ready interrupt handler | |
458 | */ | |
459 | static int ak8975_setup_irq(struct ak8975_data *data) | |
460 | { | |
461 | struct i2c_client *client = data->client; | |
462 | int rc; | |
463 | int irq; | |
464 | ||
07d2390e KK |
465 | init_waitqueue_head(&data->data_ready_queue); |
466 | clear_bit(0, &data->flags); | |
94a6d5cf JA |
467 | if (client->irq) |
468 | irq = client->irq; | |
469 | else | |
470 | irq = gpio_to_irq(data->eoc_gpio); | |
471 | ||
a845a3aa | 472 | rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, |
71222bf5 GG |
473 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
474 | dev_name(&client->dev), data); | |
94a6d5cf JA |
475 | if (rc < 0) { |
476 | dev_err(&client->dev, | |
477 | "irq %d request failed, (gpio %d): %d\n", | |
478 | irq, data->eoc_gpio, rc); | |
479 | return rc; | |
480 | } | |
481 | ||
94a6d5cf JA |
482 | data->eoc_irq = irq; |
483 | ||
484 | return rc; | |
485 | } | |
486 | ||
487 | ||
3285aae1 AC |
488 | /* |
489 | * Perform some start-of-day setup, including reading the asa calibration | |
490 | * values and caching them. | |
491 | */ | |
492 | static int ak8975_setup(struct i2c_client *client) | |
493 | { | |
40f32d93 PC |
494 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
495 | struct ak8975_data *data = iio_priv(indio_dev); | |
3285aae1 AC |
496 | int ret; |
497 | ||
3285aae1 | 498 | /* Write the fused rom access mode. */ |
286f74c2 | 499 | ret = ak8975_set_mode(data, FUSE_ROM); |
3285aae1 AC |
500 | if (ret < 0) { |
501 | dev_err(&client->dev, "Error in setting fuse access mode\n"); | |
502 | return ret; | |
503 | } | |
504 | ||
505 | /* Get asa data and store in the device data. */ | |
286f74c2 GG |
506 | ret = i2c_smbus_read_i2c_block_data(client, |
507 | data->def->ctrl_regs[ASA_BASE], | |
c411f600 | 508 | 3, data->asa); |
3285aae1 AC |
509 | if (ret < 0) { |
510 | dev_err(&client->dev, "Not able to read asa data\n"); | |
511 | return ret; | |
512 | } | |
513 | ||
040f3e58 | 514 | /* After reading fuse ROM data set power-down mode */ |
286f74c2 | 515 | ret = ak8975_set_mode(data, POWER_DOWN); |
71222bf5 GG |
516 | if (ret < 0) { |
517 | dev_err(&client->dev, "Error in setting power-down mode\n"); | |
518 | return ret; | |
519 | } | |
94a6d5cf | 520 | |
71222bf5 | 521 | if (data->eoc_gpio > 0 || client->irq > 0) { |
94a6d5cf JA |
522 | ret = ak8975_setup_irq(data); |
523 | if (ret < 0) { | |
524 | dev_err(&client->dev, | |
525 | "Error setting data ready interrupt\n"); | |
526 | return ret; | |
527 | } | |
528 | } | |
529 | ||
286f74c2 GG |
530 | data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]); |
531 | data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]); | |
532 | data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]); | |
3285aae1 AC |
533 | |
534 | return 0; | |
535 | } | |
536 | ||
01fbb478 AC |
537 | static int wait_conversion_complete_gpio(struct ak8975_data *data) |
538 | { | |
539 | struct i2c_client *client = data->client; | |
01fbb478 AC |
540 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
541 | int ret; | |
542 | ||
543 | /* Wait for the conversion to complete. */ | |
544 | while (timeout_ms) { | |
545 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); | |
546 | if (gpio_get_value(data->eoc_gpio)) | |
547 | break; | |
548 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; | |
549 | } | |
550 | if (!timeout_ms) { | |
551 | dev_err(&client->dev, "Conversion timeout happened\n"); | |
552 | return -EINVAL; | |
553 | } | |
554 | ||
286f74c2 | 555 | ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]); |
c411f600 | 556 | if (ret < 0) |
01fbb478 | 557 | dev_err(&client->dev, "Error in reading ST1\n"); |
c411f600 JC |
558 | |
559 | return ret; | |
01fbb478 AC |
560 | } |
561 | ||
562 | static int wait_conversion_complete_polled(struct ak8975_data *data) | |
563 | { | |
564 | struct i2c_client *client = data->client; | |
565 | u8 read_status; | |
566 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; | |
567 | int ret; | |
568 | ||
569 | /* Wait for the conversion to complete. */ | |
570 | while (timeout_ms) { | |
571 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); | |
286f74c2 GG |
572 | ret = i2c_smbus_read_byte_data(client, |
573 | data->def->ctrl_regs[ST1]); | |
01fbb478 AC |
574 | if (ret < 0) { |
575 | dev_err(&client->dev, "Error in reading ST1\n"); | |
576 | return ret; | |
577 | } | |
c411f600 | 578 | read_status = ret; |
01fbb478 AC |
579 | if (read_status) |
580 | break; | |
581 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; | |
582 | } | |
583 | if (!timeout_ms) { | |
584 | dev_err(&client->dev, "Conversion timeout happened\n"); | |
585 | return -EINVAL; | |
586 | } | |
94a6d5cf | 587 | |
01fbb478 AC |
588 | return read_status; |
589 | } | |
590 | ||
94a6d5cf JA |
591 | /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ |
592 | static int wait_conversion_complete_interrupt(struct ak8975_data *data) | |
593 | { | |
594 | int ret; | |
595 | ||
596 | ret = wait_event_timeout(data->data_ready_queue, | |
597 | test_bit(0, &data->flags), | |
598 | AK8975_DATA_READY_TIMEOUT); | |
599 | clear_bit(0, &data->flags); | |
600 | ||
601 | return ret > 0 ? 0 : -ETIME; | |
602 | } | |
603 | ||
3285aae1 AC |
604 | /* |
605 | * Emits the raw flux value for the x, y, or z axis. | |
606 | */ | |
694e1b5f | 607 | static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) |
3285aae1 | 608 | { |
338473c8 | 609 | struct ak8975_data *data = iio_priv(indio_dev); |
3285aae1 | 610 | struct i2c_client *client = data->client; |
3285aae1 AC |
611 | int ret; |
612 | ||
613 | mutex_lock(&data->lock); | |
614 | ||
3285aae1 | 615 | /* Set up the device for taking a sample. */ |
286f74c2 | 616 | ret = ak8975_set_mode(data, MODE_ONCE); |
3285aae1 AC |
617 | if (ret < 0) { |
618 | dev_err(&client->dev, "Error in setting operating mode\n"); | |
619 | goto exit; | |
620 | } | |
621 | ||
622 | /* Wait for the conversion to complete. */ | |
94a6d5cf JA |
623 | if (data->eoc_irq) |
624 | ret = wait_conversion_complete_interrupt(data); | |
625 | else if (gpio_is_valid(data->eoc_gpio)) | |
01fbb478 AC |
626 | ret = wait_conversion_complete_gpio(data); |
627 | else | |
628 | ret = wait_conversion_complete_polled(data); | |
629 | if (ret < 0) | |
3285aae1 | 630 | goto exit; |
3285aae1 | 631 | |
94a6d5cf | 632 | /* This will be executed only for non-interrupt based waiting case */ |
286f74c2 GG |
633 | if (ret & data->def->ctrl_masks[ST1_DRDY]) { |
634 | ret = i2c_smbus_read_byte_data(client, | |
635 | data->def->ctrl_regs[ST2]); | |
3285aae1 AC |
636 | if (ret < 0) { |
637 | dev_err(&client->dev, "Error in reading ST2\n"); | |
638 | goto exit; | |
639 | } | |
286f74c2 GG |
640 | if (ret & (data->def->ctrl_masks[ST2_DERR] | |
641 | data->def->ctrl_masks[ST2_HOFL])) { | |
c411f600 | 642 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); |
3285aae1 AC |
643 | ret = -EINVAL; |
644 | goto exit; | |
645 | } | |
646 | } | |
647 | ||
648 | /* Read the flux value from the appropriate register | |
649 | (the register is specified in the iio device attributes). */ | |
286f74c2 | 650 | ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]); |
3285aae1 AC |
651 | if (ret < 0) { |
652 | dev_err(&client->dev, "Read axis data fails\n"); | |
653 | goto exit; | |
654 | } | |
655 | ||
656 | mutex_unlock(&data->lock); | |
657 | ||
3285aae1 | 658 | /* Clamp to valid range. */ |
286f74c2 | 659 | *val = clamp_t(s16, ret, -data->def->range, data->def->range); |
694e1b5f | 660 | return IIO_VAL_INT; |
3285aae1 AC |
661 | |
662 | exit: | |
663 | mutex_unlock(&data->lock); | |
664 | return ret; | |
665 | } | |
666 | ||
694e1b5f JC |
667 | static int ak8975_read_raw(struct iio_dev *indio_dev, |
668 | struct iio_chan_spec const *chan, | |
669 | int *val, int *val2, | |
670 | long mask) | |
671 | { | |
672 | struct ak8975_data *data = iio_priv(indio_dev); | |
673 | ||
674 | switch (mask) { | |
4d9948b3 | 675 | case IIO_CHAN_INFO_RAW: |
694e1b5f | 676 | return ak8975_read_axis(indio_dev, chan->address, val); |
c8a9f805 | 677 | case IIO_CHAN_INFO_SCALE: |
bef44abc BS |
678 | *val = 0; |
679 | *val2 = data->raw_to_gauss[chan->address]; | |
680 | return IIO_VAL_INT_PLUS_MICRO; | |
694e1b5f JC |
681 | } |
682 | return -EINVAL; | |
683 | } | |
684 | ||
685 | #define AK8975_CHANNEL(axis, index) \ | |
686 | { \ | |
687 | .type = IIO_MAGN, \ | |
688 | .modified = 1, \ | |
689 | .channel2 = IIO_MOD_##axis, \ | |
3a0b4422 JC |
690 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
691 | BIT(IIO_CHAN_INFO_SCALE), \ | |
694e1b5f JC |
692 | .address = index, \ |
693 | } | |
694 | ||
695 | static const struct iio_chan_spec ak8975_channels[] = { | |
696 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), | |
697 | }; | |
698 | ||
6fe8135f | 699 | static const struct iio_info ak8975_info = { |
694e1b5f | 700 | .read_raw = &ak8975_read_raw, |
6fe8135f JC |
701 | .driver_module = THIS_MODULE, |
702 | }; | |
703 | ||
d913971e SP |
704 | static const struct acpi_device_id ak_acpi_match[] = { |
705 | {"AK8975", AK8975}, | |
706 | {"AK8963", AK8963}, | |
707 | {"INVN6500", AK8963}, | |
57e73a42 GG |
708 | {"AK09911", AK09911}, |
709 | {"AK09912", AK09912}, | |
d913971e SP |
710 | { }, |
711 | }; | |
712 | MODULE_DEVICE_TABLE(acpi, ak_acpi_match); | |
713 | ||
48edc374 IT |
714 | static const char *ak8975_match_acpi_device(struct device *dev, |
715 | enum asahi_compass_chipset *chipset) | |
d913971e SP |
716 | { |
717 | const struct acpi_device_id *id; | |
718 | ||
719 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | |
720 | if (!id) | |
721 | return NULL; | |
722 | *chipset = (int)id->driver_data; | |
723 | ||
48edc374 | 724 | return dev_name(dev); |
d913971e SP |
725 | } |
726 | ||
4ae1c61f | 727 | static int ak8975_probe(struct i2c_client *client, |
3285aae1 AC |
728 | const struct i2c_device_id *id) |
729 | { | |
730 | struct ak8975_data *data; | |
338473c8 JC |
731 | struct iio_dev *indio_dev; |
732 | int eoc_gpio; | |
3285aae1 | 733 | int err; |
48edc374 | 734 | const char *name = NULL; |
05be8d41 | 735 | enum asahi_compass_chipset chipset = AK_MAX_TYPE; |
3285aae1 | 736 | |
3285aae1 | 737 | /* Grab and set up the supplied GPIO. */ |
f4b7f751 | 738 | if (client->dev.platform_data) |
f6d838d7 | 739 | eoc_gpio = *(int *)(client->dev.platform_data); |
f4b7f751 JA |
740 | else if (client->dev.of_node) |
741 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); | |
742 | else | |
743 | eoc_gpio = -1; | |
744 | ||
745 | if (eoc_gpio == -EPROBE_DEFER) | |
746 | return -EPROBE_DEFER; | |
3285aae1 | 747 | |
01fbb478 AC |
748 | /* We may not have a GPIO based IRQ to scan, that is fine, we will |
749 | poll if so */ | |
7c6c9368 | 750 | if (gpio_is_valid(eoc_gpio)) { |
a845a3aa BS |
751 | err = devm_gpio_request_one(&client->dev, eoc_gpio, |
752 | GPIOF_IN, "ak_8975"); | |
01fbb478 AC |
753 | if (err < 0) { |
754 | dev_err(&client->dev, | |
755 | "failed to request GPIO %d, error %d\n", | |
338473c8 | 756 | eoc_gpio, err); |
a845a3aa | 757 | return err; |
01fbb478 | 758 | } |
7c6c9368 | 759 | } |
3285aae1 | 760 | |
338473c8 | 761 | /* Register with IIO */ |
a845a3aa BS |
762 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
763 | if (indio_dev == NULL) | |
764 | return -ENOMEM; | |
765 | ||
338473c8 | 766 | data = iio_priv(indio_dev); |
40f32d93 | 767 | i2c_set_clientdata(client, indio_dev); |
94a6d5cf JA |
768 | |
769 | data->client = client; | |
770 | data->eoc_gpio = eoc_gpio; | |
771 | data->eoc_irq = 0; | |
772 | ||
d913971e SP |
773 | /* id will be NULL when enumerated via ACPI */ |
774 | if (id) { | |
286f74c2 | 775 | chipset = (enum asahi_compass_chipset)(id->driver_data); |
48edc374 | 776 | name = id->name; |
d913971e | 777 | } else if (ACPI_HANDLE(&client->dev)) |
286f74c2 | 778 | name = ak8975_match_acpi_device(&client->dev, &chipset); |
a845a3aa BS |
779 | else |
780 | return -ENOSYS; | |
781 | ||
286f74c2 GG |
782 | if (chipset >= AK_MAX_TYPE) { |
783 | dev_err(&client->dev, "AKM device type unsupported: %d\n", | |
784 | chipset); | |
785 | return -ENODEV; | |
786 | } | |
787 | ||
788 | data->def = &ak_def_array[chipset]; | |
57e73a42 GG |
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 | } | |
d913971e | 794 | dev_dbg(&client->dev, "Asahi compass chip %s\n", name); |
6027c077 | 795 | |
3285aae1 AC |
796 | /* Perform some basic start-of-day setup of the device. */ |
797 | err = ak8975_setup(client); | |
798 | if (err < 0) { | |
71222bf5 | 799 | dev_err(&client->dev, "%s initialization fails\n", name); |
a845a3aa | 800 | return err; |
3285aae1 AC |
801 | } |
802 | ||
338473c8 | 803 | mutex_init(&data->lock); |
338473c8 | 804 | indio_dev->dev.parent = &client->dev; |
694e1b5f JC |
805 | indio_dev->channels = ak8975_channels; |
806 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); | |
338473c8 JC |
807 | indio_dev->info = &ak8975_info; |
808 | indio_dev->modes = INDIO_DIRECT_MODE; | |
d913971e | 809 | indio_dev->name = name; |
71222bf5 | 810 | return devm_iio_device_register(&client->dev, indio_dev); |
3285aae1 AC |
811 | } |
812 | ||
813 | static const struct i2c_device_id ak8975_id[] = { | |
6027c077 SP |
814 | {"ak8975", AK8975}, |
815 | {"ak8963", AK8963}, | |
2d8339d0 | 816 | {"AK8963", AK8963}, |
57e73a42 GG |
817 | {"ak09911", AK09911}, |
818 | {"ak09912", AK09912}, | |
3285aae1 AC |
819 | {} |
820 | }; | |
821 | ||
822 | MODULE_DEVICE_TABLE(i2c, ak8975_id); | |
823 | ||
54461c30 OJ |
824 | static const struct of_device_id ak8975_of_match[] = { |
825 | { .compatible = "asahi-kasei,ak8975", }, | |
826 | { .compatible = "ak8975", }, | |
286f74c2 GG |
827 | { .compatible = "asahi-kasei,ak8963", }, |
828 | { .compatible = "ak8963", }, | |
57e73a42 GG |
829 | { .compatible = "asahi-kasei,ak09911", }, |
830 | { .compatible = "ak09911", }, | |
831 | { .compatible = "asahi-kasei,ak09912", }, | |
832 | { .compatible = "ak09912", }, | |
286f74c2 | 833 | {} |
54461c30 OJ |
834 | }; |
835 | MODULE_DEVICE_TABLE(of, ak8975_of_match); | |
836 | ||
3285aae1 AC |
837 | static struct i2c_driver ak8975_driver = { |
838 | .driver = { | |
839 | .name = "ak8975", | |
71222bf5 | 840 | .of_match_table = of_match_ptr(ak8975_of_match), |
d913971e | 841 | .acpi_match_table = ACPI_PTR(ak_acpi_match), |
3285aae1 AC |
842 | }, |
843 | .probe = ak8975_probe, | |
3285aae1 AC |
844 | .id_table = ak8975_id, |
845 | }; | |
6e5af184 | 846 | module_i2c_driver(ak8975_driver); |
3285aae1 AC |
847 | |
848 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
849 | MODULE_DESCRIPTION("AK8975 magnetometer driver"); | |
850 | MODULE_LICENSE("GPL"); |