blob: 05423543f89d7896ba1a6f086b409955c35417fa [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>
Andrew Chew3285aae2010-09-08 22:02:17 -070034
Jonathan Cameron06458e22012-04-25 15:54:58 +010035#include <linux/iio/iio.h>
36#include <linux/iio/sysfs.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070037/*
38 * Register definitions, as well as various shifts and masks to get at the
39 * individual fields of the registers.
40 */
41#define AK8975_REG_WIA 0x00
42#define AK8975_DEVICE_ID 0x48
43
44#define AK8975_REG_INFO 0x01
45
46#define AK8975_REG_ST1 0x02
47#define AK8975_REG_ST1_DRDY_SHIFT 0
48#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
49
50#define AK8975_REG_HXL 0x03
51#define AK8975_REG_HXH 0x04
52#define AK8975_REG_HYL 0x05
53#define AK8975_REG_HYH 0x06
54#define AK8975_REG_HZL 0x07
55#define AK8975_REG_HZH 0x08
56#define AK8975_REG_ST2 0x09
57#define AK8975_REG_ST2_DERR_SHIFT 2
58#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
59
60#define AK8975_REG_ST2_HOFL_SHIFT 3
61#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
62
63#define AK8975_REG_CNTL 0x0A
64#define AK8975_REG_CNTL_MODE_SHIFT 0
65#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
66#define AK8975_REG_CNTL_MODE_POWER_DOWN 0
67#define AK8975_REG_CNTL_MODE_ONCE 1
68#define AK8975_REG_CNTL_MODE_SELF_TEST 8
69#define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
70
71#define AK8975_REG_RSVC 0x0B
72#define AK8975_REG_ASTC 0x0C
73#define AK8975_REG_TS1 0x0D
74#define AK8975_REG_TS2 0x0E
75#define AK8975_REG_I2CDIS 0x0F
76#define AK8975_REG_ASAX 0x10
77#define AK8975_REG_ASAY 0x11
78#define AK8975_REG_ASAZ 0x12
79
80#define AK8975_MAX_REGS AK8975_REG_ASAZ
81
82/*
83 * Miscellaneous values.
84 */
85#define AK8975_MAX_CONVERSION_TIMEOUT 500
86#define AK8975_CONVERSION_DONE_POLL_TIME 10
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010087#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
Beomho Seobef44ab2014-04-02 09:15:00 +010088#define RAW_TO_GAUSS(asa) ((((asa) + 128) * 3000) / 256)
Andrew Chew3285aae2010-09-08 22:02:17 -070089
90/*
91 * Per-instance context data for the device.
92 */
93struct ak8975_data {
94 struct i2c_client *client;
Andrew Chew3285aae2010-09-08 22:02:17 -070095 struct attribute_group attrs;
96 struct mutex lock;
97 u8 asa[3];
98 long raw_to_gauss[3];
Andrew Chew3285aae2010-09-08 22:02:17 -070099 u8 reg_cache[AK8975_MAX_REGS];
100 int eoc_gpio;
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100101 int eoc_irq;
102 wait_queue_head_t data_ready_queue;
103 unsigned long flags;
Andrew Chew3285aae2010-09-08 22:02:17 -0700104};
105
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100106static const int ak8975_index_to_reg[] = {
107 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
108};
109
Andrew Chew3285aae2010-09-08 22:02:17 -0700110/*
111 * Helper function to write to the I2C device's registers.
112 */
113static int ak8975_write_data(struct i2c_client *client,
114 u8 reg, u8 val, u8 mask, u8 shift)
115{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530116 struct iio_dev *indio_dev = i2c_get_clientdata(client);
117 struct ak8975_data *data = iio_priv(indio_dev);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100118 u8 regval;
119 int ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700120
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100121 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
122 ret = i2c_smbus_write_byte_data(client, reg, regval);
Andrew Chew3285aae2010-09-08 22:02:17 -0700123 if (ret < 0) {
124 dev_err(&client->dev, "Write to device fails status %x\n", ret);
125 return ret;
126 }
127 data->reg_cache[reg] = regval;
128
129 return 0;
130}
131
132/*
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100133 * Handle data ready irq
134 */
135static irqreturn_t ak8975_irq_handler(int irq, void *data)
136{
137 struct ak8975_data *ak8975 = data;
138
139 set_bit(0, &ak8975->flags);
140 wake_up(&ak8975->data_ready_queue);
141
142 return IRQ_HANDLED;
143}
144
145/*
146 * Install data ready interrupt handler
147 */
148static int ak8975_setup_irq(struct ak8975_data *data)
149{
150 struct i2c_client *client = data->client;
151 int rc;
152 int irq;
153
154 if (client->irq)
155 irq = client->irq;
156 else
157 irq = gpio_to_irq(data->eoc_gpio);
158
159 rc = request_irq(irq, ak8975_irq_handler,
160 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
161 dev_name(&client->dev), data);
162 if (rc < 0) {
163 dev_err(&client->dev,
164 "irq %d request failed, (gpio %d): %d\n",
165 irq, data->eoc_gpio, rc);
166 return rc;
167 }
168
169 init_waitqueue_head(&data->data_ready_queue);
170 clear_bit(0, &data->flags);
171 data->eoc_irq = irq;
172
173 return rc;
174}
175
176
177/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700178 * Perform some start-of-day setup, including reading the asa calibration
179 * values and caching them.
180 */
181static int ak8975_setup(struct i2c_client *client)
182{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530183 struct iio_dev *indio_dev = i2c_get_clientdata(client);
184 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700185 u8 device_id;
186 int ret;
187
188 /* Confirm that the device we're talking to is really an AK8975. */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000189 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
Andrew Chew3285aae2010-09-08 22:02:17 -0700190 if (ret < 0) {
191 dev_err(&client->dev, "Error reading WIA\n");
192 return ret;
193 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000194 device_id = ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700195 if (device_id != AK8975_DEVICE_ID) {
196 dev_err(&client->dev, "Device ak8975 not found\n");
197 return -ENODEV;
198 }
199
200 /* Write the fused rom access mode. */
201 ret = ak8975_write_data(client,
202 AK8975_REG_CNTL,
203 AK8975_REG_CNTL_MODE_FUSE_ROM,
204 AK8975_REG_CNTL_MODE_MASK,
205 AK8975_REG_CNTL_MODE_SHIFT);
206 if (ret < 0) {
207 dev_err(&client->dev, "Error in setting fuse access mode\n");
208 return ret;
209 }
210
211 /* Get asa data and store in the device data. */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000212 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
213 3, data->asa);
Andrew Chew3285aae2010-09-08 22:02:17 -0700214 if (ret < 0) {
215 dev_err(&client->dev, "Not able to read asa data\n");
216 return ret;
217 }
218
Leed Aguilar040f3e52012-06-06 16:14:56 -0400219 /* After reading fuse ROM data set power-down mode */
220 ret = ak8975_write_data(client,
221 AK8975_REG_CNTL,
222 AK8975_REG_CNTL_MODE_POWER_DOWN,
223 AK8975_REG_CNTL_MODE_MASK,
224 AK8975_REG_CNTL_MODE_SHIFT);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100225
226 if (data->eoc_gpio > 0 || client->irq) {
227 ret = ak8975_setup_irq(data);
228 if (ret < 0) {
229 dev_err(&client->dev,
230 "Error setting data ready interrupt\n");
231 return ret;
232 }
233 }
234
Leed Aguilar040f3e52012-06-06 16:14:56 -0400235 if (ret < 0) {
236 dev_err(&client->dev, "Error in setting power-down mode\n");
237 return ret;
238 }
239
Andrew Chew3285aae2010-09-08 22:02:17 -0700240/*
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100241 * Precalculate scale factor (in Gauss units) for each axis and
242 * store in the device data.
Andrew Chew3285aae2010-09-08 22:02:17 -0700243 *
244 * This scale factor is axis-dependent, and is derived from 3 calibration
245 * factors ASA(x), ASA(y), and ASA(z).
246 *
247 * These ASA values are read from the sensor device at start of day, and
248 * cached in the device context struct.
249 *
250 * Adjusting the flux value with the sensitivity adjustment value should be
251 * done via the following formula:
252 *
253 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
254 *
255 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
256 * is the resultant adjusted value.
257 *
258 * We reduce the formula to:
259 *
260 * Hadj = H * (ASA + 128) / 256
261 *
262 * H is in the range of -4096 to 4095. The magnetometer has a range of
263 * +-1229uT. To go from the raw value to uT is:
264 *
265 * HuT = H * 1229/4096, or roughly, 3/10.
266 *
Peter Meerwaldeb03610a2013-10-19 18:17:00 +0100267 * Since 1uT = 0.01 gauss, our final scale factor becomes:
Andrew Chew3285aae2010-09-08 22:02:17 -0700268 *
Beomho Seobef44ab2014-04-02 09:15:00 +0100269 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
270 * Hadj = H * ((ASA + 128) * 0.003) / 256
Andrew Chew3285aae2010-09-08 22:02:17 -0700271 *
272 * Since ASA doesn't change, we cache the resultant scale factor into the
273 * device context in ak8975_setup().
274 */
Beomho Seobef44ab2014-04-02 09:15:00 +0100275 data->raw_to_gauss[0] = RAW_TO_GAUSS(data->asa[0]);
276 data->raw_to_gauss[1] = RAW_TO_GAUSS(data->asa[1]);
277 data->raw_to_gauss[2] = RAW_TO_GAUSS(data->asa[2]);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100278
279 return 0;
280}
281
Alan Cox01fbb472011-04-06 13:31:40 +0100282static int wait_conversion_complete_gpio(struct ak8975_data *data)
283{
284 struct i2c_client *client = data->client;
Alan Cox01fbb472011-04-06 13:31:40 +0100285 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
286 int ret;
287
288 /* Wait for the conversion to complete. */
289 while (timeout_ms) {
290 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
291 if (gpio_get_value(data->eoc_gpio))
292 break;
293 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
294 }
295 if (!timeout_ms) {
296 dev_err(&client->dev, "Conversion timeout happened\n");
297 return -EINVAL;
298 }
299
Jonathan Cameronc411f602013-03-24 16:58:00 +0000300 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
301 if (ret < 0)
Alan Cox01fbb472011-04-06 13:31:40 +0100302 dev_err(&client->dev, "Error in reading ST1\n");
Jonathan Cameronc411f602013-03-24 16:58:00 +0000303
304 return ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100305}
306
307static int wait_conversion_complete_polled(struct ak8975_data *data)
308{
309 struct i2c_client *client = data->client;
310 u8 read_status;
311 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
312 int ret;
313
314 /* Wait for the conversion to complete. */
315 while (timeout_ms) {
316 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
Jonathan Cameronc411f602013-03-24 16:58:00 +0000317 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
Alan Cox01fbb472011-04-06 13:31:40 +0100318 if (ret < 0) {
319 dev_err(&client->dev, "Error in reading ST1\n");
320 return ret;
321 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000322 read_status = ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100323 if (read_status)
324 break;
325 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
326 }
327 if (!timeout_ms) {
328 dev_err(&client->dev, "Conversion timeout happened\n");
329 return -EINVAL;
330 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100331
Alan Cox01fbb472011-04-06 13:31:40 +0100332 return read_status;
333}
334
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100335/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
336static int wait_conversion_complete_interrupt(struct ak8975_data *data)
337{
338 int ret;
339
340 ret = wait_event_timeout(data->data_ready_queue,
341 test_bit(0, &data->flags),
342 AK8975_DATA_READY_TIMEOUT);
343 clear_bit(0, &data->flags);
344
345 return ret > 0 ? 0 : -ETIME;
346}
347
Andrew Chew3285aae2010-09-08 22:02:17 -0700348/*
349 * Emits the raw flux value for the x, y, or z axis.
350 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100351static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700352{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100353 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700354 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700355 u16 meas_reg;
356 s16 raw;
Andrew Chew3285aae2010-09-08 22:02:17 -0700357 int ret;
358
359 mutex_lock(&data->lock);
360
Andrew Chew3285aae2010-09-08 22:02:17 -0700361 /* Set up the device for taking a sample. */
362 ret = ak8975_write_data(client,
363 AK8975_REG_CNTL,
364 AK8975_REG_CNTL_MODE_ONCE,
365 AK8975_REG_CNTL_MODE_MASK,
366 AK8975_REG_CNTL_MODE_SHIFT);
367 if (ret < 0) {
368 dev_err(&client->dev, "Error in setting operating mode\n");
369 goto exit;
370 }
371
372 /* Wait for the conversion to complete. */
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100373 if (data->eoc_irq)
374 ret = wait_conversion_complete_interrupt(data);
375 else if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100376 ret = wait_conversion_complete_gpio(data);
377 else
378 ret = wait_conversion_complete_polled(data);
379 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700380 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700381
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100382 /* This will be executed only for non-interrupt based waiting case */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000383 if (ret & AK8975_REG_ST1_DRDY_MASK) {
384 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
Andrew Chew3285aae2010-09-08 22:02:17 -0700385 if (ret < 0) {
386 dev_err(&client->dev, "Error in reading ST2\n");
387 goto exit;
388 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000389 if (ret & (AK8975_REG_ST2_DERR_MASK |
390 AK8975_REG_ST2_HOFL_MASK)) {
391 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
Andrew Chew3285aae2010-09-08 22:02:17 -0700392 ret = -EINVAL;
393 goto exit;
394 }
395 }
396
397 /* Read the flux value from the appropriate register
398 (the register is specified in the iio device attributes). */
Jonathan Cameronc411f602013-03-24 16:58:00 +0000399 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700400 if (ret < 0) {
401 dev_err(&client->dev, "Read axis data fails\n");
402 goto exit;
403 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000404 meas_reg = ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700405
406 mutex_unlock(&data->lock);
407
408 /* Endian conversion of the measured values. */
409 raw = (s16) (le16_to_cpu(meas_reg));
410
411 /* Clamp to valid range. */
412 raw = clamp_t(s16, raw, -4096, 4095);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100413 *val = raw;
414 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700415
416exit:
417 mutex_unlock(&data->lock);
418 return ret;
419}
420
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100421static int ak8975_read_raw(struct iio_dev *indio_dev,
422 struct iio_chan_spec const *chan,
423 int *val, int *val2,
424 long mask)
425{
426 struct ak8975_data *data = iio_priv(indio_dev);
427
428 switch (mask) {
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100429 case IIO_CHAN_INFO_RAW:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100430 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100431 case IIO_CHAN_INFO_SCALE:
Beomho Seobef44ab2014-04-02 09:15:00 +0100432 *val = 0;
433 *val2 = data->raw_to_gauss[chan->address];
434 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100435 }
436 return -EINVAL;
437}
438
439#define AK8975_CHANNEL(axis, index) \
440 { \
441 .type = IIO_MAGN, \
442 .modified = 1, \
443 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron3a0b4422013-02-27 19:40:48 +0000444 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
445 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100446 .address = index, \
447 }
448
449static const struct iio_chan_spec ak8975_channels[] = {
450 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
451};
452
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100453static const struct iio_info ak8975_info = {
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100454 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100455 .driver_module = THIS_MODULE,
456};
457
Bill Pemberton4ae1c61f2012-11-19 13:21:57 -0500458static int ak8975_probe(struct i2c_client *client,
Andrew Chew3285aae2010-09-08 22:02:17 -0700459 const struct i2c_device_id *id)
460{
461 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100462 struct iio_dev *indio_dev;
463 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700464 int err;
465
Andrew Chew3285aae2010-09-08 22:02:17 -0700466 /* Grab and set up the supplied GPIO. */
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100467 if (client->dev.platform_data)
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100468 eoc_gpio = *(int *)(client->dev.platform_data);
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100469 else if (client->dev.of_node)
470 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
471 else
472 eoc_gpio = -1;
473
474 if (eoc_gpio == -EPROBE_DEFER)
475 return -EPROBE_DEFER;
Andrew Chew3285aae2010-09-08 22:02:17 -0700476
Alan Cox01fbb472011-04-06 13:31:40 +0100477 /* We may not have a GPIO based IRQ to scan, that is fine, we will
478 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100479 if (gpio_is_valid(eoc_gpio)) {
Leed Aguilar82f2acd2012-06-06 16:15:40 -0400480 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100481 if (err < 0) {
482 dev_err(&client->dev,
483 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100484 eoc_gpio, err);
485 goto exit;
Alan Cox01fbb472011-04-06 13:31:40 +0100486 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100487 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700488
Jonathan Cameron338473c2011-06-27 13:07:54 +0100489 /* Register with IIO */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200490 indio_dev = iio_device_alloc(sizeof(*data));
Jonathan Cameron338473c2011-06-27 13:07:54 +0100491 if (indio_dev == NULL) {
492 err = -ENOMEM;
493 goto exit_gpio;
494 }
495 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530496 i2c_set_clientdata(client, indio_dev);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100497
498 data->client = client;
499 data->eoc_gpio = eoc_gpio;
500 data->eoc_irq = 0;
501
Andrew Chew3285aae2010-09-08 22:02:17 -0700502 /* Perform some basic start-of-day setup of the device. */
503 err = ak8975_setup(client);
504 if (err < 0) {
505 dev_err(&client->dev, "AK8975 initialization fails\n");
Stephen Warrenad31d252011-09-21 11:16:01 +0100506 goto exit_free_iio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700507 }
508
Jonathan Cameron338473c2011-06-27 13:07:54 +0100509 data->client = client;
510 mutex_init(&data->lock);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100511 data->eoc_gpio = eoc_gpio;
512 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100513 indio_dev->channels = ak8975_channels;
514 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100515 indio_dev->info = &ak8975_info;
516 indio_dev->modes = INDIO_DIRECT_MODE;
Andrew Chew3285aae2010-09-08 22:02:17 -0700517
Jonathan Cameron338473c2011-06-27 13:07:54 +0100518 err = iio_device_register(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700519 if (err < 0)
520 goto exit_free_iio;
521
522 return 0;
523
524exit_free_iio:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200525 iio_device_free(indio_dev);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100526 if (data->eoc_irq)
527 free_irq(data->eoc_irq, data);
Andrew Chew3285aae2010-09-08 22:02:17 -0700528exit_gpio:
Stephen Warren7c6c9362011-09-21 11:16:00 +0100529 if (gpio_is_valid(eoc_gpio))
Jonathan Cameron338473c2011-06-27 13:07:54 +0100530 gpio_free(eoc_gpio);
Andrew Chew3285aae2010-09-08 22:02:17 -0700531exit:
532 return err;
533}
534
Bill Pemberton447d4f22012-11-19 13:26:37 -0500535static int ak8975_remove(struct i2c_client *client)
Andrew Chew3285aae2010-09-08 22:02:17 -0700536{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100537 struct iio_dev *indio_dev = i2c_get_clientdata(client);
538 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700539
Jonathan Cameron338473c2011-06-27 13:07:54 +0100540 iio_device_unregister(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700541
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100542 if (data->eoc_irq)
543 free_irq(data->eoc_irq, data);
544
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100545 if (gpio_is_valid(data->eoc_gpio))
546 gpio_free(data->eoc_gpio);
547
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200548 iio_device_free(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700549
550 return 0;
551}
552
553static const struct i2c_device_id ak8975_id[] = {
554 {"ak8975", 0},
555 {}
556};
557
558MODULE_DEVICE_TABLE(i2c, ak8975_id);
559
Olof Johansson54461c32011-12-22 18:46:42 -0800560static const struct of_device_id ak8975_of_match[] = {
561 { .compatible = "asahi-kasei,ak8975", },
562 { .compatible = "ak8975", },
563 { }
564};
565MODULE_DEVICE_TABLE(of, ak8975_of_match);
566
Andrew Chew3285aae2010-09-08 22:02:17 -0700567static struct i2c_driver ak8975_driver = {
568 .driver = {
569 .name = "ak8975",
Olof Johansson54461c32011-12-22 18:46:42 -0800570 .of_match_table = ak8975_of_match,
Andrew Chew3285aae2010-09-08 22:02:17 -0700571 },
572 .probe = ak8975_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500573 .remove = ak8975_remove,
Andrew Chew3285aae2010-09-08 22:02:17 -0700574 .id_table = ak8975_id,
575};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100576module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700577
578MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
579MODULE_DESCRIPTION("AK8975 magnetometer driver");
580MODULE_LICENSE("GPL");