blob: 4e69480e67b5c16bc4e44dcf8a7a4938cc8f840a [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>
Andrew Chew3285aae2010-09-08 22:02:17 -070035
Jonathan Cameron06458e22012-04-25 15:54:58 +010036#include <linux/iio/iio.h>
37#include <linux/iio/sysfs.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070038/*
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)
Gwendal Grignou71222bf2014-11-21 10:45:47 -080067#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
Andrew Chew3285aae2010-09-08 22:02:17 -070071
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
83/*
84 * Miscellaneous values.
85 */
86#define AK8975_MAX_CONVERSION_TIMEOUT 500
87#define AK8975_CONVERSION_DONE_POLL_TIME 10
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010088#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
Srinivas Pandruvada6027c072014-03-19 16:56:00 +000089#define RAW_TO_GAUSS_8975(asa) ((((asa) + 128) * 3000) / 256)
90#define RAW_TO_GAUSS_8963(asa) ((((asa) + 128) * 6000) / 256)
91
92/* Compatible Asahi Kasei Compass parts */
93enum asahi_compass_chipset {
94 AK8975,
95 AK8963,
96};
Andrew Chew3285aae2010-09-08 22:02:17 -070097
98/*
99 * Per-instance context data for the device.
100 */
101struct ak8975_data {
102 struct i2c_client *client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700103 struct attribute_group attrs;
104 struct mutex lock;
105 u8 asa[3];
106 long raw_to_gauss[3];
Andrew Chew3285aae2010-09-08 22:02:17 -0700107 u8 reg_cache[AK8975_MAX_REGS];
108 int eoc_gpio;
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100109 int eoc_irq;
110 wait_queue_head_t data_ready_queue;
111 unsigned long flags;
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000112 enum asahi_compass_chipset chipset;
Andrew Chew3285aae2010-09-08 22:02:17 -0700113};
114
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100115static const int ak8975_index_to_reg[] = {
116 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
117};
118
Andrew Chew3285aae2010-09-08 22:02:17 -0700119/*
120 * Helper function to write to the I2C device's registers.
121 */
122static int ak8975_write_data(struct i2c_client *client,
123 u8 reg, u8 val, u8 mask, u8 shift)
124{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530125 struct iio_dev *indio_dev = i2c_get_clientdata(client);
126 struct ak8975_data *data = iio_priv(indio_dev);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100127 u8 regval;
128 int ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700129
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100130 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
131 ret = i2c_smbus_write_byte_data(client, reg, regval);
Andrew Chew3285aae2010-09-08 22:02:17 -0700132 if (ret < 0) {
133 dev_err(&client->dev, "Write to device fails status %x\n", ret);
134 return ret;
135 }
136 data->reg_cache[reg] = regval;
137
138 return 0;
139}
140
141/*
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100142 * Handle data ready irq
143 */
144static irqreturn_t ak8975_irq_handler(int irq, void *data)
145{
146 struct ak8975_data *ak8975 = data;
147
148 set_bit(0, &ak8975->flags);
149 wake_up(&ak8975->data_ready_queue);
150
151 return IRQ_HANDLED;
152}
153
154/*
155 * Install data ready interrupt handler
156 */
157static int ak8975_setup_irq(struct ak8975_data *data)
158{
159 struct i2c_client *client = data->client;
160 int rc;
161 int irq;
162
163 if (client->irq)
164 irq = client->irq;
165 else
166 irq = gpio_to_irq(data->eoc_gpio);
167
Beomho Seoa845a3a2014-06-23 13:34:00 +0100168 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800169 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
170 dev_name(&client->dev), data);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100171 if (rc < 0) {
172 dev_err(&client->dev,
173 "irq %d request failed, (gpio %d): %d\n",
174 irq, data->eoc_gpio, rc);
175 return rc;
176 }
177
178 init_waitqueue_head(&data->data_ready_queue);
179 clear_bit(0, &data->flags);
180 data->eoc_irq = irq;
181
182 return rc;
183}
184
185
186/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700187 * Perform some start-of-day setup, including reading the asa calibration
188 * values and caching them.
189 */
190static int ak8975_setup(struct i2c_client *client)
191{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530192 struct iio_dev *indio_dev = i2c_get_clientdata(client);
193 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700194 u8 device_id;
195 int ret;
196
197 /* Confirm that the device we're talking to is really an AK8975. */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000198 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
Andrew Chew3285aae2010-09-08 22:02:17 -0700199 if (ret < 0) {
200 dev_err(&client->dev, "Error reading WIA\n");
201 return ret;
202 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000203 device_id = ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700204 if (device_id != AK8975_DEVICE_ID) {
205 dev_err(&client->dev, "Device ak8975 not found\n");
206 return -ENODEV;
207 }
208
209 /* Write the fused rom access mode. */
210 ret = ak8975_write_data(client,
211 AK8975_REG_CNTL,
212 AK8975_REG_CNTL_MODE_FUSE_ROM,
213 AK8975_REG_CNTL_MODE_MASK,
214 AK8975_REG_CNTL_MODE_SHIFT);
215 if (ret < 0) {
216 dev_err(&client->dev, "Error in setting fuse access mode\n");
217 return ret;
218 }
219
220 /* Get asa data and store in the device data. */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000221 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
222 3, data->asa);
Andrew Chew3285aae2010-09-08 22:02:17 -0700223 if (ret < 0) {
224 dev_err(&client->dev, "Not able to read asa data\n");
225 return ret;
226 }
227
Leed Aguilar040f3e52012-06-06 16:14:56 -0400228 /* After reading fuse ROM data set power-down mode */
229 ret = ak8975_write_data(client,
230 AK8975_REG_CNTL,
231 AK8975_REG_CNTL_MODE_POWER_DOWN,
232 AK8975_REG_CNTL_MODE_MASK,
233 AK8975_REG_CNTL_MODE_SHIFT);
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800234 if (ret < 0) {
235 dev_err(&client->dev, "Error in setting power-down mode\n");
236 return ret;
237 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100238
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800239 if (data->eoc_gpio > 0 || client->irq > 0) {
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100240 ret = ak8975_setup_irq(data);
241 if (ret < 0) {
242 dev_err(&client->dev,
243 "Error setting data ready interrupt\n");
244 return ret;
245 }
246 }
247
Andrew Chew3285aae2010-09-08 22:02:17 -0700248/*
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100249 * Precalculate scale factor (in Gauss units) for each axis and
250 * store in the device data.
Andrew Chew3285aae2010-09-08 22:02:17 -0700251 *
252 * This scale factor is axis-dependent, and is derived from 3 calibration
253 * factors ASA(x), ASA(y), and ASA(z).
254 *
255 * These ASA values are read from the sensor device at start of day, and
256 * cached in the device context struct.
257 *
258 * Adjusting the flux value with the sensitivity adjustment value should be
259 * done via the following formula:
260 *
261 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
262 *
263 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
264 * is the resultant adjusted value.
265 *
266 * We reduce the formula to:
267 *
268 * Hadj = H * (ASA + 128) / 256
269 *
270 * H is in the range of -4096 to 4095. The magnetometer has a range of
271 * +-1229uT. To go from the raw value to uT is:
272 *
273 * HuT = H * 1229/4096, or roughly, 3/10.
274 *
Peter Meerwaldeb036102013-10-19 18:17:00 +0100275 * Since 1uT = 0.01 gauss, our final scale factor becomes:
Andrew Chew3285aae2010-09-08 22:02:17 -0700276 *
Beomho Seobef44ab2014-04-02 09:15:00 +0100277 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
278 * Hadj = H * ((ASA + 128) * 0.003) / 256
Andrew Chew3285aae2010-09-08 22:02:17 -0700279 *
280 * Since ASA doesn't change, we cache the resultant scale factor into the
281 * device context in ak8975_setup().
282 */
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000283 if (data->chipset == AK8963) {
284 /*
285 * H range is +-8190 and magnetometer range is +-4912.
286 * So HuT using the above explanation for 8975,
287 * 4912/8190 = ~ 6/10.
288 * So the Hadj should use 6/10 instead of 3/10.
289 */
290 data->raw_to_gauss[0] = RAW_TO_GAUSS_8963(data->asa[0]);
291 data->raw_to_gauss[1] = RAW_TO_GAUSS_8963(data->asa[1]);
292 data->raw_to_gauss[2] = RAW_TO_GAUSS_8963(data->asa[2]);
293 } else {
294 data->raw_to_gauss[0] = RAW_TO_GAUSS_8975(data->asa[0]);
295 data->raw_to_gauss[1] = RAW_TO_GAUSS_8975(data->asa[1]);
296 data->raw_to_gauss[2] = RAW_TO_GAUSS_8975(data->asa[2]);
297 }
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100298
299 return 0;
300}
301
Alan Cox01fbb472011-04-06 13:31:40 +0100302static int wait_conversion_complete_gpio(struct ak8975_data *data)
303{
304 struct i2c_client *client = data->client;
Alan Cox01fbb472011-04-06 13:31:40 +0100305 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
306 int ret;
307
308 /* Wait for the conversion to complete. */
309 while (timeout_ms) {
310 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
311 if (gpio_get_value(data->eoc_gpio))
312 break;
313 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
314 }
315 if (!timeout_ms) {
316 dev_err(&client->dev, "Conversion timeout happened\n");
317 return -EINVAL;
318 }
319
Jonathan Cameronc411f602013-03-24 16:58:00 +0000320 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
321 if (ret < 0)
Alan Cox01fbb472011-04-06 13:31:40 +0100322 dev_err(&client->dev, "Error in reading ST1\n");
Jonathan Cameronc411f602013-03-24 16:58:00 +0000323
324 return ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100325}
326
327static int wait_conversion_complete_polled(struct ak8975_data *data)
328{
329 struct i2c_client *client = data->client;
330 u8 read_status;
331 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
332 int ret;
333
334 /* Wait for the conversion to complete. */
335 while (timeout_ms) {
336 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
Jonathan Cameronc411f602013-03-24 16:58:00 +0000337 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
Alan Cox01fbb472011-04-06 13:31:40 +0100338 if (ret < 0) {
339 dev_err(&client->dev, "Error in reading ST1\n");
340 return ret;
341 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000342 read_status = ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100343 if (read_status)
344 break;
345 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
346 }
347 if (!timeout_ms) {
348 dev_err(&client->dev, "Conversion timeout happened\n");
349 return -EINVAL;
350 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100351
Alan Cox01fbb472011-04-06 13:31:40 +0100352 return read_status;
353}
354
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100355/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
356static int wait_conversion_complete_interrupt(struct ak8975_data *data)
357{
358 int ret;
359
360 ret = wait_event_timeout(data->data_ready_queue,
361 test_bit(0, &data->flags),
362 AK8975_DATA_READY_TIMEOUT);
363 clear_bit(0, &data->flags);
364
365 return ret > 0 ? 0 : -ETIME;
366}
367
Andrew Chew3285aae2010-09-08 22:02:17 -0700368/*
369 * Emits the raw flux value for the x, y, or z axis.
370 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100371static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700372{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100373 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700374 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700375 int ret;
376
377 mutex_lock(&data->lock);
378
Andrew Chew3285aae2010-09-08 22:02:17 -0700379 /* Set up the device for taking a sample. */
380 ret = ak8975_write_data(client,
381 AK8975_REG_CNTL,
382 AK8975_REG_CNTL_MODE_ONCE,
383 AK8975_REG_CNTL_MODE_MASK,
384 AK8975_REG_CNTL_MODE_SHIFT);
385 if (ret < 0) {
386 dev_err(&client->dev, "Error in setting operating mode\n");
387 goto exit;
388 }
389
390 /* Wait for the conversion to complete. */
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100391 if (data->eoc_irq)
392 ret = wait_conversion_complete_interrupt(data);
393 else if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100394 ret = wait_conversion_complete_gpio(data);
395 else
396 ret = wait_conversion_complete_polled(data);
397 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700398 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700399
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100400 /* This will be executed only for non-interrupt based waiting case */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000401 if (ret & AK8975_REG_ST1_DRDY_MASK) {
402 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
Andrew Chew3285aae2010-09-08 22:02:17 -0700403 if (ret < 0) {
404 dev_err(&client->dev, "Error in reading ST2\n");
405 goto exit;
406 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000407 if (ret & (AK8975_REG_ST2_DERR_MASK |
408 AK8975_REG_ST2_HOFL_MASK)) {
409 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
Andrew Chew3285aae2010-09-08 22:02:17 -0700410 ret = -EINVAL;
411 goto exit;
412 }
413 }
414
415 /* Read the flux value from the appropriate register
416 (the register is specified in the iio device attributes). */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000417 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700418 if (ret < 0) {
419 dev_err(&client->dev, "Read axis data fails\n");
420 goto exit;
421 }
422
423 mutex_unlock(&data->lock);
424
Andrew Chew3285aae2010-09-08 22:02:17 -0700425 /* Clamp to valid range. */
Peter Meerwald8ba42fb2014-05-06 09:53:00 +0100426 *val = clamp_t(s16, ret, -4096, 4095);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100427 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700428
429exit:
430 mutex_unlock(&data->lock);
431 return ret;
432}
433
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100434static int ak8975_read_raw(struct iio_dev *indio_dev,
435 struct iio_chan_spec const *chan,
436 int *val, int *val2,
437 long mask)
438{
439 struct ak8975_data *data = iio_priv(indio_dev);
440
441 switch (mask) {
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100442 case IIO_CHAN_INFO_RAW:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100443 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100444 case IIO_CHAN_INFO_SCALE:
Beomho Seobef44ab2014-04-02 09:15:00 +0100445 *val = 0;
446 *val2 = data->raw_to_gauss[chan->address];
447 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100448 }
449 return -EINVAL;
450}
451
452#define AK8975_CHANNEL(axis, index) \
453 { \
454 .type = IIO_MAGN, \
455 .modified = 1, \
456 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron3a0b4422013-02-27 19:40:48 +0000457 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
458 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100459 .address = index, \
460 }
461
462static const struct iio_chan_spec ak8975_channels[] = {
463 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
464};
465
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100466static const struct iio_info ak8975_info = {
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100467 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100468 .driver_module = THIS_MODULE,
469};
470
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000471static const struct acpi_device_id ak_acpi_match[] = {
472 {"AK8975", AK8975},
473 {"AK8963", AK8963},
474 {"INVN6500", AK8963},
475 { },
476};
477MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
478
Irina Tirdea48edc372014-08-09 15:18:00 +0100479static const char *ak8975_match_acpi_device(struct device *dev,
480 enum asahi_compass_chipset *chipset)
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000481{
482 const struct acpi_device_id *id;
483
484 id = acpi_match_device(dev->driver->acpi_match_table, dev);
485 if (!id)
486 return NULL;
487 *chipset = (int)id->driver_data;
488
Irina Tirdea48edc372014-08-09 15:18:00 +0100489 return dev_name(dev);
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000490}
491
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500492static int ak8975_probe(struct i2c_client *client,
Andrew Chew3285aae2010-09-08 22:02:17 -0700493 const struct i2c_device_id *id)
494{
495 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100496 struct iio_dev *indio_dev;
497 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700498 int err;
Irina Tirdea48edc372014-08-09 15:18:00 +0100499 const char *name = NULL;
Andrew Chew3285aae2010-09-08 22:02:17 -0700500
Andrew Chew3285aae2010-09-08 22:02:17 -0700501 /* Grab and set up the supplied GPIO. */
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100502 if (client->dev.platform_data)
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100503 eoc_gpio = *(int *)(client->dev.platform_data);
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100504 else if (client->dev.of_node)
505 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
506 else
507 eoc_gpio = -1;
508
509 if (eoc_gpio == -EPROBE_DEFER)
510 return -EPROBE_DEFER;
Andrew Chew3285aae2010-09-08 22:02:17 -0700511
Alan Cox01fbb472011-04-06 13:31:40 +0100512 /* We may not have a GPIO based IRQ to scan, that is fine, we will
513 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100514 if (gpio_is_valid(eoc_gpio)) {
Beomho Seoa845a3a2014-06-23 13:34:00 +0100515 err = devm_gpio_request_one(&client->dev, eoc_gpio,
516 GPIOF_IN, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100517 if (err < 0) {
518 dev_err(&client->dev,
519 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100520 eoc_gpio, err);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100521 return err;
Alan Cox01fbb472011-04-06 13:31:40 +0100522 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100523 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700524
Jonathan Cameron338473c2011-06-27 13:07:54 +0100525 /* Register with IIO */
Beomho Seoa845a3a2014-06-23 13:34:00 +0100526 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
527 if (indio_dev == NULL)
528 return -ENOMEM;
529
Jonathan Cameron338473c2011-06-27 13:07:54 +0100530 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530531 i2c_set_clientdata(client, indio_dev);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100532
533 data->client = client;
534 data->eoc_gpio = eoc_gpio;
535 data->eoc_irq = 0;
536
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000537 /* id will be NULL when enumerated via ACPI */
538 if (id) {
539 data->chipset =
540 (enum asahi_compass_chipset)(id->driver_data);
Irina Tirdea48edc372014-08-09 15:18:00 +0100541 name = id->name;
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000542 } else if (ACPI_HANDLE(&client->dev))
543 name = ak8975_match_acpi_device(&client->dev, &data->chipset);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100544 else
545 return -ENOSYS;
546
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000547 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000548
Andrew Chew3285aae2010-09-08 22:02:17 -0700549 /* Perform some basic start-of-day setup of the device. */
550 err = ak8975_setup(client);
551 if (err < 0) {
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800552 dev_err(&client->dev, "%s initialization fails\n", name);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100553 return err;
Andrew Chew3285aae2010-09-08 22:02:17 -0700554 }
555
Jonathan Cameron338473c2011-06-27 13:07:54 +0100556 mutex_init(&data->lock);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100557 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100558 indio_dev->channels = ak8975_channels;
559 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100560 indio_dev->info = &ak8975_info;
561 indio_dev->modes = INDIO_DIRECT_MODE;
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000562 indio_dev->name = name;
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800563 return devm_iio_device_register(&client->dev, indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700564}
565
566static const struct i2c_device_id ak8975_id[] = {
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000567 {"ak8975", AK8975},
568 {"ak8963", AK8963},
Andrew Chew3285aae2010-09-08 22:02:17 -0700569 {}
570};
571
572MODULE_DEVICE_TABLE(i2c, ak8975_id);
573
Olof Johansson54461c32011-12-22 18:46:42 -0800574static const struct of_device_id ak8975_of_match[] = {
575 { .compatible = "asahi-kasei,ak8975", },
576 { .compatible = "ak8975", },
577 { }
578};
579MODULE_DEVICE_TABLE(of, ak8975_of_match);
580
Andrew Chew3285aae2010-09-08 22:02:17 -0700581static struct i2c_driver ak8975_driver = {
582 .driver = {
583 .name = "ak8975",
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800584 .of_match_table = of_match_ptr(ak8975_of_match),
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000585 .acpi_match_table = ACPI_PTR(ak_acpi_match),
Andrew Chew3285aae2010-09-08 22:02:17 -0700586 },
587 .probe = ak8975_probe,
Andrew Chew3285aae2010-09-08 22:02:17 -0700588 .id_table = ak8975_id,
589};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100590module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700591
592MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
593MODULE_DESCRIPTION("AK8975 magnetometer driver");
594MODULE_LICENSE("GPL");