blob: 48d127a45d90d9dfd05d05e239f117db462d3b04 [file] [log] [blame]
Andrew Chew3285aae2010-09-08 22:02:17 -07001/*
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>
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010027#include <linux/interrupt.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070028#include <linux/err.h>
29#include <linux/mutex.h>
30#include <linux/delay.h>
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010031#include <linux/bitops.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070032#include <linux/gpio.h>
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +010033#include <linux/of_gpio.h>
Srinivas Pandruvadad9139712014-03-19 16:56:00 +000034#include <linux/acpi.h>
Gregor Boirie63d5d522016-03-03 11:44:05 +010035#include <linux/regulator/consumer.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070036
Jonathan Cameron06458e22012-04-25 15:54:58 +010037#include <linux/iio/iio.h>
38#include <linux/iio/sysfs.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070039/*
40 * Register definitions, as well as various shifts and masks to get at the
41 * individual fields of the registers.
42 */
43#define AK8975_REG_WIA 0x00
44#define AK8975_DEVICE_ID 0x48
45
46#define AK8975_REG_INFO 0x01
47
48#define AK8975_REG_ST1 0x02
49#define AK8975_REG_ST1_DRDY_SHIFT 0
50#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
51
52#define AK8975_REG_HXL 0x03
53#define AK8975_REG_HXH 0x04
54#define AK8975_REG_HYL 0x05
55#define AK8975_REG_HYH 0x06
56#define AK8975_REG_HZL 0x07
57#define AK8975_REG_HZH 0x08
58#define AK8975_REG_ST2 0x09
59#define AK8975_REG_ST2_DERR_SHIFT 2
60#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
61
62#define AK8975_REG_ST2_HOFL_SHIFT 3
63#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
64
65#define AK8975_REG_CNTL 0x0A
66#define AK8975_REG_CNTL_MODE_SHIFT 0
67#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
Gwendal Grignou71222bf2014-11-21 10:45:47 -080068#define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
69#define AK8975_REG_CNTL_MODE_ONCE 0x01
70#define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
71#define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
Andrew Chew3285aae2010-09-08 22:02:17 -070072
73#define AK8975_REG_RSVC 0x0B
74#define AK8975_REG_ASTC 0x0C
75#define AK8975_REG_TS1 0x0D
76#define AK8975_REG_TS2 0x0E
77#define AK8975_REG_I2CDIS 0x0F
78#define AK8975_REG_ASAX 0x10
79#define AK8975_REG_ASAY 0x11
80#define AK8975_REG_ASAZ 0x12
81
82#define AK8975_MAX_REGS AK8975_REG_ASAZ
83
84/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -080085 * AK09912 Register definitions
86 */
87#define AK09912_REG_WIA1 0x00
88#define AK09912_REG_WIA2 0x01
89#define AK09912_DEVICE_ID 0x04
90#define AK09911_DEVICE_ID 0x05
91
92#define AK09911_REG_INFO1 0x02
93#define AK09911_REG_INFO2 0x03
94
95#define AK09912_REG_ST1 0x10
96
97#define AK09912_REG_ST1_DRDY_SHIFT 0
98#define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
99
100#define AK09912_REG_HXL 0x11
101#define AK09912_REG_HXH 0x12
102#define AK09912_REG_HYL 0x13
103#define AK09912_REG_HYH 0x14
104#define AK09912_REG_HZL 0x15
105#define AK09912_REG_HZH 0x16
106#define AK09912_REG_TMPS 0x17
107
108#define AK09912_REG_ST2 0x18
109#define AK09912_REG_ST2_HOFL_SHIFT 3
110#define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
111
112#define AK09912_REG_CNTL1 0x30
113
114#define AK09912_REG_CNTL2 0x31
115#define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
116#define AK09912_REG_CNTL_MODE_ONCE 0x01
117#define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
118#define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
119#define AK09912_REG_CNTL2_MODE_SHIFT 0
120#define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
121
122#define AK09912_REG_CNTL3 0x32
123
124#define AK09912_REG_TS1 0x33
125#define AK09912_REG_TS2 0x34
126#define AK09912_REG_TS3 0x35
127#define AK09912_REG_I2CDIS 0x36
128#define AK09912_REG_TS4 0x37
129
130#define AK09912_REG_ASAX 0x60
131#define AK09912_REG_ASAY 0x61
132#define AK09912_REG_ASAZ 0x62
133
134#define AK09912_MAX_REGS AK09912_REG_ASAZ
135
136/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700137 * Miscellaneous values.
138 */
139#define AK8975_MAX_CONVERSION_TIMEOUT 500
140#define AK8975_CONVERSION_DONE_POLL_TIME 10
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100141#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800142
143/*
144 * Precalculate scale factor (in Gauss units) for each axis and
145 * store in the device data.
146 *
147 * This scale factor is axis-dependent, and is derived from 3 calibration
148 * factors ASA(x), ASA(y), and ASA(z).
149 *
150 * These ASA values are read from the sensor device at start of day, and
151 * cached in the device context struct.
152 *
153 * Adjusting the flux value with the sensitivity adjustment value should be
154 * done via the following formula:
155 *
156 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
157 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
158 * is the resultant adjusted value.
159 *
160 * We reduce the formula to:
161 *
162 * Hadj = H * (ASA + 128) / 256
163 *
164 * H is in the range of -4096 to 4095. The magnetometer has a range of
165 * +-1229uT. To go from the raw value to uT is:
166 *
167 * HuT = H * 1229/4096, or roughly, 3/10.
168 *
169 * Since 1uT = 0.01 gauss, our final scale factor becomes:
170 *
171 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
172 * Hadj = H * ((ASA + 128) * 0.003) / 256
173 *
174 * Since ASA doesn't change, we cache the resultant scale factor into the
175 * device context in ak8975_setup().
176 *
177 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
178 * multiply the stored scale value by 1e6.
179 */
180static long ak8975_raw_to_gauss(u16 data)
181{
182 return (((long)data + 128) * 3000) / 256;
183}
184
185/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800186 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800187 *
188 * H is in the range of +-8190. The magnetometer has a range of
189 * +-4912uT. To go from the raw value to uT is:
190 *
191 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
192 */
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800193
194static long ak8963_09911_raw_to_gauss(u16 data)
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800195{
196 return (((long)data + 128) * 6000) / 256;
197}
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000198
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800199/*
200 * For AK09912, same calculation, except the device is more sensitive:
201 *
202 * H is in the range of -32752 to 32752. The magnetometer has a range of
203 * +-4912uT. To go from the raw value to uT is:
204 *
205 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
206 */
207static long ak09912_raw_to_gauss(u16 data)
208{
209 return (((long)data + 128) * 1500) / 256;
210}
211
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000212/* Compatible Asahi Kasei Compass parts */
213enum asahi_compass_chipset {
214 AK8975,
215 AK8963,
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800216 AK09911,
217 AK09912,
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800218 AK_MAX_TYPE
219};
220
221enum ak_ctrl_reg_addr {
222 ST1,
223 ST2,
224 CNTL,
225 ASA_BASE,
226 MAX_REGS,
227 REGS_END,
228};
229
230enum ak_ctrl_reg_mask {
231 ST1_DRDY,
232 ST2_HOFL,
233 ST2_DERR,
234 CNTL_MODE,
235 MASK_END,
236};
237
238enum ak_ctrl_mode {
239 POWER_DOWN,
240 MODE_ONCE,
241 SELF_TEST,
242 FUSE_ROM,
243 MODE_END,
244};
245
246struct ak_def {
247 enum asahi_compass_chipset type;
248 long (*raw_to_gauss)(u16 data);
249 u16 range;
250 u8 ctrl_regs[REGS_END];
251 u8 ctrl_masks[MASK_END];
252 u8 ctrl_modes[MODE_END];
253 u8 data_regs[3];
254};
255
Julia Lawallf78c5f92016-01-05 15:56:42 +0100256static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800257 {
258 .type = AK8975,
259 .raw_to_gauss = ak8975_raw_to_gauss,
260 .range = 4096,
261 .ctrl_regs = {
262 AK8975_REG_ST1,
263 AK8975_REG_ST2,
264 AK8975_REG_CNTL,
265 AK8975_REG_ASAX,
266 AK8975_MAX_REGS},
267 .ctrl_masks = {
268 AK8975_REG_ST1_DRDY_MASK,
269 AK8975_REG_ST2_HOFL_MASK,
270 AK8975_REG_ST2_DERR_MASK,
271 AK8975_REG_CNTL_MODE_MASK},
272 .ctrl_modes = {
273 AK8975_REG_CNTL_MODE_POWER_DOWN,
274 AK8975_REG_CNTL_MODE_ONCE,
275 AK8975_REG_CNTL_MODE_SELF_TEST,
276 AK8975_REG_CNTL_MODE_FUSE_ROM},
277 .data_regs = {
278 AK8975_REG_HXL,
279 AK8975_REG_HYL,
280 AK8975_REG_HZL},
281 },
282 {
283 .type = AK8963,
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800284 .raw_to_gauss = ak8963_09911_raw_to_gauss,
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800285 .range = 8190,
286 .ctrl_regs = {
287 AK8975_REG_ST1,
288 AK8975_REG_ST2,
289 AK8975_REG_CNTL,
290 AK8975_REG_ASAX,
291 AK8975_MAX_REGS},
292 .ctrl_masks = {
293 AK8975_REG_ST1_DRDY_MASK,
294 AK8975_REG_ST2_HOFL_MASK,
295 0,
296 AK8975_REG_CNTL_MODE_MASK},
297 .ctrl_modes = {
298 AK8975_REG_CNTL_MODE_POWER_DOWN,
299 AK8975_REG_CNTL_MODE_ONCE,
300 AK8975_REG_CNTL_MODE_SELF_TEST,
301 AK8975_REG_CNTL_MODE_FUSE_ROM},
302 .data_regs = {
303 AK8975_REG_HXL,
304 AK8975_REG_HYL,
305 AK8975_REG_HZL},
306 },
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800307 {
308 .type = AK09911,
309 .raw_to_gauss = ak8963_09911_raw_to_gauss,
310 .range = 8192,
311 .ctrl_regs = {
312 AK09912_REG_ST1,
313 AK09912_REG_ST2,
314 AK09912_REG_CNTL2,
315 AK09912_REG_ASAX,
316 AK09912_MAX_REGS},
317 .ctrl_masks = {
318 AK09912_REG_ST1_DRDY_MASK,
319 AK09912_REG_ST2_HOFL_MASK,
320 0,
321 AK09912_REG_CNTL2_MODE_MASK},
322 .ctrl_modes = {
323 AK09912_REG_CNTL_MODE_POWER_DOWN,
324 AK09912_REG_CNTL_MODE_ONCE,
325 AK09912_REG_CNTL_MODE_SELF_TEST,
326 AK09912_REG_CNTL_MODE_FUSE_ROM},
327 .data_regs = {
328 AK09912_REG_HXL,
329 AK09912_REG_HYL,
330 AK09912_REG_HZL},
331 },
332 {
333 .type = AK09912,
334 .raw_to_gauss = ak09912_raw_to_gauss,
335 .range = 32752,
336 .ctrl_regs = {
337 AK09912_REG_ST1,
338 AK09912_REG_ST2,
339 AK09912_REG_CNTL2,
340 AK09912_REG_ASAX,
341 AK09912_MAX_REGS},
342 .ctrl_masks = {
343 AK09912_REG_ST1_DRDY_MASK,
344 AK09912_REG_ST2_HOFL_MASK,
345 0,
346 AK09912_REG_CNTL2_MODE_MASK},
347 .ctrl_modes = {
348 AK09912_REG_CNTL_MODE_POWER_DOWN,
349 AK09912_REG_CNTL_MODE_ONCE,
350 AK09912_REG_CNTL_MODE_SELF_TEST,
351 AK09912_REG_CNTL_MODE_FUSE_ROM},
352 .data_regs = {
353 AK09912_REG_HXL,
354 AK09912_REG_HYL,
355 AK09912_REG_HZL},
356 }
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000357};
Andrew Chew3285aae2010-09-08 22:02:17 -0700358
359/*
360 * Per-instance context data for the device.
361 */
362struct ak8975_data {
363 struct i2c_client *client;
Julia Lawallf78c5f92016-01-05 15:56:42 +0100364 const struct ak_def *def;
Andrew Chew3285aae2010-09-08 22:02:17 -0700365 struct mutex lock;
366 u8 asa[3];
367 long raw_to_gauss[3];
Andrew Chew3285aae2010-09-08 22:02:17 -0700368 int eoc_gpio;
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100369 int eoc_irq;
370 wait_queue_head_t data_ready_queue;
371 unsigned long flags;
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800372 u8 cntl_cache;
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100373 struct regulator *vdd;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100374};
375
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100376/* Enable attached power regulator if any. */
377static int ak8975_power_on(struct i2c_client *client)
378{
379 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
380 struct ak8975_data *data = iio_priv(indio_dev);
381 int ret;
382
383 data->vdd = devm_regulator_get(&client->dev, "vdd");
384 if (IS_ERR_OR_NULL(data->vdd)) {
385 ret = PTR_ERR(data->vdd);
386 if (ret == -ENODEV)
387 ret = 0;
388 } else {
389 ret = regulator_enable(data->vdd);
390 }
391
392 if (ret)
393 dev_err(&client->dev, "failed to enable Vdd supply: %d\n", ret);
394 return ret;
395}
396
397/* Disable attached power regulator if any. */
398static void ak8975_power_off(const struct i2c_client *client)
399{
400 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
401 const struct ak8975_data *data = iio_priv(indio_dev);
402
403 if (!IS_ERR_OR_NULL(data->vdd))
404 regulator_disable(data->vdd);
405}
406
Andrew Chew3285aae2010-09-08 22:02:17 -0700407/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800408 * Return 0 if the i2c device is the one we expect.
409 * return a negative error number otherwise
410 */
411static int ak8975_who_i_am(struct i2c_client *client,
412 enum asahi_compass_chipset type)
413{
414 u8 wia_val[2];
415 int ret;
416
417 /*
418 * Signature for each device:
419 * Device | WIA1 | WIA2
420 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
421 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
422 * AK8975 | DEVICE_ID | NA
423 * AK8963 | DEVICE_ID | NA
424 */
425 ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
426 2, wia_val);
427 if (ret < 0) {
428 dev_err(&client->dev, "Error reading WIA\n");
429 return ret;
430 }
431
432 if (wia_val[0] != AK8975_DEVICE_ID)
433 return -ENODEV;
434
435 switch (type) {
436 case AK8975:
437 case AK8963:
438 return 0;
439 case AK09911:
440 if (wia_val[1] == AK09911_DEVICE_ID)
441 return 0;
442 break;
443 case AK09912:
444 if (wia_val[1] == AK09912_DEVICE_ID)
445 return 0;
446 break;
447 default:
448 dev_err(&client->dev, "Type %d unknown\n", type);
449 }
450 return -ENODEV;
451}
452
453/*
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800454 * Helper function to write to CNTL register.
Andrew Chew3285aae2010-09-08 22:02:17 -0700455 */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800456static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
Andrew Chew3285aae2010-09-08 22:02:17 -0700457{
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100458 u8 regval;
459 int ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700460
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800461 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
462 data->def->ctrl_modes[mode];
463 ret = i2c_smbus_write_byte_data(data->client,
464 data->def->ctrl_regs[CNTL], regval);
Andrew Chew3285aae2010-09-08 22:02:17 -0700465 if (ret < 0) {
Andrew Chew3285aae2010-09-08 22:02:17 -0700466 return ret;
467 }
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800468 data->cntl_cache = regval;
469 /* After mode change wait atleast 100us */
470 usleep_range(100, 500);
Andrew Chew3285aae2010-09-08 22:02:17 -0700471
472 return 0;
473}
474
475/*
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100476 * Handle data ready irq
477 */
478static irqreturn_t ak8975_irq_handler(int irq, void *data)
479{
480 struct ak8975_data *ak8975 = data;
481
482 set_bit(0, &ak8975->flags);
483 wake_up(&ak8975->data_ready_queue);
484
485 return IRQ_HANDLED;
486}
487
488/*
489 * Install data ready interrupt handler
490 */
491static int ak8975_setup_irq(struct ak8975_data *data)
492{
493 struct i2c_client *client = data->client;
494 int rc;
495 int irq;
496
497 if (client->irq)
498 irq = client->irq;
499 else
500 irq = gpio_to_irq(data->eoc_gpio);
501
Beomho Seoa845a3a2014-06-23 13:34:00 +0100502 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800503 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
504 dev_name(&client->dev), data);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100505 if (rc < 0) {
506 dev_err(&client->dev,
507 "irq %d request failed, (gpio %d): %d\n",
508 irq, data->eoc_gpio, rc);
509 return rc;
510 }
511
512 init_waitqueue_head(&data->data_ready_queue);
513 clear_bit(0, &data->flags);
514 data->eoc_irq = irq;
515
516 return rc;
517}
518
519
520/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700521 * Perform some start-of-day setup, including reading the asa calibration
522 * values and caching them.
523 */
524static int ak8975_setup(struct i2c_client *client)
525{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530526 struct iio_dev *indio_dev = i2c_get_clientdata(client);
527 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700528 int ret;
529
Andrew Chew3285aae2010-09-08 22:02:17 -0700530 /* Write the fused rom access mode. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800531 ret = ak8975_set_mode(data, FUSE_ROM);
Andrew Chew3285aae2010-09-08 22:02:17 -0700532 if (ret < 0) {
533 dev_err(&client->dev, "Error in setting fuse access mode\n");
534 return ret;
535 }
536
537 /* Get asa data and store in the device data. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800538 ret = i2c_smbus_read_i2c_block_data(client,
539 data->def->ctrl_regs[ASA_BASE],
Jonathan Cameronc411f602013-03-24 16:58:00 +0000540 3, data->asa);
Andrew Chew3285aae2010-09-08 22:02:17 -0700541 if (ret < 0) {
542 dev_err(&client->dev, "Not able to read asa data\n");
543 return ret;
544 }
545
Leed Aguilar040f3e52012-06-06 16:14:56 -0400546 /* After reading fuse ROM data set power-down mode */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800547 ret = ak8975_set_mode(data, POWER_DOWN);
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800548 if (ret < 0) {
549 dev_err(&client->dev, "Error in setting power-down mode\n");
550 return ret;
551 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100552
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800553 if (data->eoc_gpio > 0 || client->irq > 0) {
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100554 ret = ak8975_setup_irq(data);
555 if (ret < 0) {
556 dev_err(&client->dev,
557 "Error setting data ready interrupt\n");
558 return ret;
559 }
560 }
561
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800562 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
563 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
564 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100565
566 return 0;
567}
568
Alan Cox01fbb472011-04-06 13:31:40 +0100569static int wait_conversion_complete_gpio(struct ak8975_data *data)
570{
571 struct i2c_client *client = data->client;
Alan Cox01fbb472011-04-06 13:31:40 +0100572 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
573 int ret;
574
575 /* Wait for the conversion to complete. */
576 while (timeout_ms) {
577 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
578 if (gpio_get_value(data->eoc_gpio))
579 break;
580 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
581 }
582 if (!timeout_ms) {
583 dev_err(&client->dev, "Conversion timeout happened\n");
584 return -EINVAL;
585 }
586
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800587 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
Jonathan Cameronc411f602013-03-24 16:58:00 +0000588 if (ret < 0)
Alan Cox01fbb472011-04-06 13:31:40 +0100589 dev_err(&client->dev, "Error in reading ST1\n");
Jonathan Cameronc411f602013-03-24 16:58:00 +0000590
591 return ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100592}
593
594static int wait_conversion_complete_polled(struct ak8975_data *data)
595{
596 struct i2c_client *client = data->client;
597 u8 read_status;
598 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
599 int ret;
600
601 /* Wait for the conversion to complete. */
602 while (timeout_ms) {
603 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800604 ret = i2c_smbus_read_byte_data(client,
605 data->def->ctrl_regs[ST1]);
Alan Cox01fbb472011-04-06 13:31:40 +0100606 if (ret < 0) {
607 dev_err(&client->dev, "Error in reading ST1\n");
608 return ret;
609 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000610 read_status = ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100611 if (read_status)
612 break;
613 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
614 }
615 if (!timeout_ms) {
616 dev_err(&client->dev, "Conversion timeout happened\n");
617 return -EINVAL;
618 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100619
Alan Cox01fbb472011-04-06 13:31:40 +0100620 return read_status;
621}
622
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100623/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
624static int wait_conversion_complete_interrupt(struct ak8975_data *data)
625{
626 int ret;
627
628 ret = wait_event_timeout(data->data_ready_queue,
629 test_bit(0, &data->flags),
630 AK8975_DATA_READY_TIMEOUT);
631 clear_bit(0, &data->flags);
632
633 return ret > 0 ? 0 : -ETIME;
634}
635
Andrew Chew3285aae2010-09-08 22:02:17 -0700636/*
637 * Emits the raw flux value for the x, y, or z axis.
638 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100639static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700640{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100641 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700642 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700643 int ret;
644
645 mutex_lock(&data->lock);
646
Andrew Chew3285aae2010-09-08 22:02:17 -0700647 /* Set up the device for taking a sample. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800648 ret = ak8975_set_mode(data, MODE_ONCE);
Andrew Chew3285aae2010-09-08 22:02:17 -0700649 if (ret < 0) {
650 dev_err(&client->dev, "Error in setting operating mode\n");
651 goto exit;
652 }
653
654 /* Wait for the conversion to complete. */
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100655 if (data->eoc_irq)
656 ret = wait_conversion_complete_interrupt(data);
657 else if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100658 ret = wait_conversion_complete_gpio(data);
659 else
660 ret = wait_conversion_complete_polled(data);
661 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700662 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700663
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100664 /* This will be executed only for non-interrupt based waiting case */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800665 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
666 ret = i2c_smbus_read_byte_data(client,
667 data->def->ctrl_regs[ST2]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700668 if (ret < 0) {
669 dev_err(&client->dev, "Error in reading ST2\n");
670 goto exit;
671 }
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800672 if (ret & (data->def->ctrl_masks[ST2_DERR] |
673 data->def->ctrl_masks[ST2_HOFL])) {
Jonathan Cameronc411f602013-03-24 16:58:00 +0000674 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
Andrew Chew3285aae2010-09-08 22:02:17 -0700675 ret = -EINVAL;
676 goto exit;
677 }
678 }
679
680 /* Read the flux value from the appropriate register
681 (the register is specified in the iio device attributes). */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800682 ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700683 if (ret < 0) {
684 dev_err(&client->dev, "Read axis data fails\n");
685 goto exit;
686 }
687
688 mutex_unlock(&data->lock);
689
Andrew Chew3285aae2010-09-08 22:02:17 -0700690 /* Clamp to valid range. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800691 *val = clamp_t(s16, ret, -data->def->range, data->def->range);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100692 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700693
694exit:
695 mutex_unlock(&data->lock);
696 return ret;
697}
698
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100699static int ak8975_read_raw(struct iio_dev *indio_dev,
700 struct iio_chan_spec const *chan,
701 int *val, int *val2,
702 long mask)
703{
704 struct ak8975_data *data = iio_priv(indio_dev);
705
706 switch (mask) {
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100707 case IIO_CHAN_INFO_RAW:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100708 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100709 case IIO_CHAN_INFO_SCALE:
Beomho Seobef44ab2014-04-02 09:15:00 +0100710 *val = 0;
711 *val2 = data->raw_to_gauss[chan->address];
712 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100713 }
714 return -EINVAL;
715}
716
717#define AK8975_CHANNEL(axis, index) \
718 { \
719 .type = IIO_MAGN, \
720 .modified = 1, \
721 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron3a0b4422013-02-27 19:40:48 +0000722 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
723 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100724 .address = index, \
725 }
726
727static const struct iio_chan_spec ak8975_channels[] = {
728 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
729};
730
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100731static const struct iio_info ak8975_info = {
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100732 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100733 .driver_module = THIS_MODULE,
734};
735
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000736static const struct acpi_device_id ak_acpi_match[] = {
737 {"AK8975", AK8975},
738 {"AK8963", AK8963},
739 {"INVN6500", AK8963},
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800740 {"AK09911", AK09911},
741 {"AK09912", AK09912},
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000742 { },
743};
744MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
745
Irina Tirdea48edc372014-08-09 15:18:00 +0100746static const char *ak8975_match_acpi_device(struct device *dev,
747 enum asahi_compass_chipset *chipset)
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000748{
749 const struct acpi_device_id *id;
750
751 id = acpi_match_device(dev->driver->acpi_match_table, dev);
752 if (!id)
753 return NULL;
754 *chipset = (int)id->driver_data;
755
Irina Tirdea48edc372014-08-09 15:18:00 +0100756 return dev_name(dev);
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000757}
758
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500759static int ak8975_probe(struct i2c_client *client,
Andrew Chew3285aae2010-09-08 22:02:17 -0700760 const struct i2c_device_id *id)
761{
762 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100763 struct iio_dev *indio_dev;
764 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700765 int err;
Irina Tirdea48edc372014-08-09 15:18:00 +0100766 const char *name = NULL;
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800767 enum asahi_compass_chipset chipset;
Andrew Chew3285aae2010-09-08 22:02:17 -0700768
Andrew Chew3285aae2010-09-08 22:02:17 -0700769 /* Grab and set up the supplied GPIO. */
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100770 if (client->dev.platform_data)
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100771 eoc_gpio = *(int *)(client->dev.platform_data);
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100772 else if (client->dev.of_node)
773 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
774 else
775 eoc_gpio = -1;
776
777 if (eoc_gpio == -EPROBE_DEFER)
778 return -EPROBE_DEFER;
Andrew Chew3285aae2010-09-08 22:02:17 -0700779
Alan Cox01fbb472011-04-06 13:31:40 +0100780 /* We may not have a GPIO based IRQ to scan, that is fine, we will
781 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100782 if (gpio_is_valid(eoc_gpio)) {
Beomho Seoa845a3a2014-06-23 13:34:00 +0100783 err = devm_gpio_request_one(&client->dev, eoc_gpio,
784 GPIOF_IN, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100785 if (err < 0) {
786 dev_err(&client->dev,
787 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100788 eoc_gpio, err);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100789 return err;
Alan Cox01fbb472011-04-06 13:31:40 +0100790 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100791 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700792
Jonathan Cameron338473c2011-06-27 13:07:54 +0100793 /* Register with IIO */
Beomho Seoa845a3a2014-06-23 13:34:00 +0100794 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
795 if (indio_dev == NULL)
796 return -ENOMEM;
797
Jonathan Cameron338473c2011-06-27 13:07:54 +0100798 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530799 i2c_set_clientdata(client, indio_dev);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100800
801 data->client = client;
802 data->eoc_gpio = eoc_gpio;
803 data->eoc_irq = 0;
804
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000805 /* id will be NULL when enumerated via ACPI */
806 if (id) {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800807 chipset = (enum asahi_compass_chipset)(id->driver_data);
Irina Tirdea48edc372014-08-09 15:18:00 +0100808 name = id->name;
Gregor Boirie55c0c532016-03-03 11:44:03 +0100809 } else if (ACPI_HANDLE(&client->dev)) {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800810 name = ak8975_match_acpi_device(&client->dev, &chipset);
Gregor Boirie55c0c532016-03-03 11:44:03 +0100811 if (!name)
812 return -ENODEV;
813 }
Beomho Seoa845a3a2014-06-23 13:34:00 +0100814 else
815 return -ENOSYS;
816
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800817 if (chipset >= AK_MAX_TYPE) {
818 dev_err(&client->dev, "AKM device type unsupported: %d\n",
819 chipset);
820 return -ENODEV;
821 }
822
823 data->def = &ak_def_array[chipset];
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100824
825 err = ak8975_power_on(client);
826 if (err)
827 return err;
828
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800829 err = ak8975_who_i_am(client, data->def->type);
830 if (err < 0) {
831 dev_err(&client->dev, "Unexpected device\n");
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100832 goto power_off;
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800833 }
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000834 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000835
Andrew Chew3285aae2010-09-08 22:02:17 -0700836 /* Perform some basic start-of-day setup of the device. */
837 err = ak8975_setup(client);
838 if (err < 0) {
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800839 dev_err(&client->dev, "%s initialization fails\n", name);
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100840 goto power_off;
Andrew Chew3285aae2010-09-08 22:02:17 -0700841 }
842
Jonathan Cameron338473c2011-06-27 13:07:54 +0100843 mutex_init(&data->lock);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100844 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100845 indio_dev->channels = ak8975_channels;
846 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100847 indio_dev->info = &ak8975_info;
848 indio_dev->modes = INDIO_DIRECT_MODE;
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000849 indio_dev->name = name;
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100850
851 err = iio_device_register(indio_dev);
852 if (err)
853 goto power_off;
854
855 return 0;
856
857power_off:
858 ak8975_power_off(client);
859 return err;
860}
861
862static int ak8975_remove(struct i2c_client *client)
863{
864 struct iio_dev *indio_dev = i2c_get_clientdata(client);
865
866 iio_device_unregister(indio_dev);
867 ak8975_power_off(client);
868
869 return 0;
Andrew Chew3285aae2010-09-08 22:02:17 -0700870}
871
872static const struct i2c_device_id ak8975_id[] = {
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000873 {"ak8975", AK8975},
874 {"ak8963", AK8963},
Srinivas Pandruvada2d8339d2015-01-09 15:30:38 -0800875 {"AK8963", AK8963},
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800876 {"ak09911", AK09911},
877 {"ak09912", AK09912},
Andrew Chew3285aae2010-09-08 22:02:17 -0700878 {}
879};
880
881MODULE_DEVICE_TABLE(i2c, ak8975_id);
882
Olof Johansson54461c32011-12-22 18:46:42 -0800883static const struct of_device_id ak8975_of_match[] = {
884 { .compatible = "asahi-kasei,ak8975", },
885 { .compatible = "ak8975", },
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800886 { .compatible = "asahi-kasei,ak8963", },
887 { .compatible = "ak8963", },
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800888 { .compatible = "asahi-kasei,ak09911", },
889 { .compatible = "ak09911", },
890 { .compatible = "asahi-kasei,ak09912", },
891 { .compatible = "ak09912", },
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800892 {}
Olof Johansson54461c32011-12-22 18:46:42 -0800893};
894MODULE_DEVICE_TABLE(of, ak8975_of_match);
895
Andrew Chew3285aae2010-09-08 22:02:17 -0700896static struct i2c_driver ak8975_driver = {
897 .driver = {
898 .name = "ak8975",
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800899 .of_match_table = of_match_ptr(ak8975_of_match),
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000900 .acpi_match_table = ACPI_PTR(ak_acpi_match),
Andrew Chew3285aae2010-09-08 22:02:17 -0700901 },
902 .probe = ak8975_probe,
Gregor Boiriea9b72c92016-03-17 17:43:26 +0100903 .remove = ak8975_remove,
Andrew Chew3285aae2010-09-08 22:02:17 -0700904 .id_table = ak8975_id,
905};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100906module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700907
908MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
909MODULE_DESCRIPTION("AK8975 magnetometer driver");
910MODULE_LICENSE("GPL");