blob: ebc2d0840caf4c867eb967c7ed6a8a5c93fee6f2 [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>
27#include <linux/err.h>
28#include <linux/mutex.h>
29#include <linux/delay.h>
30
31#include <linux/gpio.h>
32
33#include "../iio.h"
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010034#include "../sysfs.h"
Andrew Chew3285aae2010-09-08 22:02:17 -070035/*
36 * Register definitions, as well as various shifts and masks to get at the
37 * individual fields of the registers.
38 */
39#define AK8975_REG_WIA 0x00
40#define AK8975_DEVICE_ID 0x48
41
42#define AK8975_REG_INFO 0x01
43
44#define AK8975_REG_ST1 0x02
45#define AK8975_REG_ST1_DRDY_SHIFT 0
46#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
47
48#define AK8975_REG_HXL 0x03
49#define AK8975_REG_HXH 0x04
50#define AK8975_REG_HYL 0x05
51#define AK8975_REG_HYH 0x06
52#define AK8975_REG_HZL 0x07
53#define AK8975_REG_HZH 0x08
54#define AK8975_REG_ST2 0x09
55#define AK8975_REG_ST2_DERR_SHIFT 2
56#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
57
58#define AK8975_REG_ST2_HOFL_SHIFT 3
59#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
60
61#define AK8975_REG_CNTL 0x0A
62#define AK8975_REG_CNTL_MODE_SHIFT 0
63#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
64#define AK8975_REG_CNTL_MODE_POWER_DOWN 0
65#define AK8975_REG_CNTL_MODE_ONCE 1
66#define AK8975_REG_CNTL_MODE_SELF_TEST 8
67#define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
68
69#define AK8975_REG_RSVC 0x0B
70#define AK8975_REG_ASTC 0x0C
71#define AK8975_REG_TS1 0x0D
72#define AK8975_REG_TS2 0x0E
73#define AK8975_REG_I2CDIS 0x0F
74#define AK8975_REG_ASAX 0x10
75#define AK8975_REG_ASAY 0x11
76#define AK8975_REG_ASAZ 0x12
77
78#define AK8975_MAX_REGS AK8975_REG_ASAZ
79
80/*
81 * Miscellaneous values.
82 */
83#define AK8975_MAX_CONVERSION_TIMEOUT 500
84#define AK8975_CONVERSION_DONE_POLL_TIME 10
85
86/*
87 * Per-instance context data for the device.
88 */
89struct ak8975_data {
90 struct i2c_client *client;
Andrew Chew3285aae2010-09-08 22:02:17 -070091 struct attribute_group attrs;
92 struct mutex lock;
93 u8 asa[3];
94 long raw_to_gauss[3];
Jonathan Cameron694e1b52011-08-12 17:48:03 +010095 bool mode;
Andrew Chew3285aae2010-09-08 22:02:17 -070096 u8 reg_cache[AK8975_MAX_REGS];
97 int eoc_gpio;
98 int eoc_irq;
99};
100
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100101static const int ak8975_index_to_reg[] = {
102 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
103};
104
Andrew Chew3285aae2010-09-08 22:02:17 -0700105/*
106 * Helper function to write to the I2C device's registers.
107 */
108static int ak8975_write_data(struct i2c_client *client,
109 u8 reg, u8 val, u8 mask, u8 shift)
110{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530111 struct iio_dev *indio_dev = i2c_get_clientdata(client);
112 struct ak8975_data *data = iio_priv(indio_dev);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100113 u8 regval;
114 int ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700115
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100116 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
117 ret = i2c_smbus_write_byte_data(client, reg, regval);
Andrew Chew3285aae2010-09-08 22:02:17 -0700118 if (ret < 0) {
119 dev_err(&client->dev, "Write to device fails status %x\n", ret);
120 return ret;
121 }
122 data->reg_cache[reg] = regval;
123
124 return 0;
125}
126
127/*
128 * Helper function to read a contiguous set of the I2C device's registers.
129 */
130static int ak8975_read_data(struct i2c_client *client,
131 u8 reg, u8 length, u8 *buffer)
132{
Andrew Chew3285aae2010-09-08 22:02:17 -0700133 int ret;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100134 struct i2c_msg msg[2] = {
135 {
136 .addr = client->addr,
137 .flags = I2C_M_NOSTART,
138 .len = 1,
139 .buf = &reg,
140 }, {
141 .addr = client->addr,
142 .flags = I2C_M_RD,
143 .len = length,
144 .buf = buffer,
145 }
146 };
Andrew Chew3285aae2010-09-08 22:02:17 -0700147
148 ret = i2c_transfer(client->adapter, msg, 2);
149 if (ret < 0) {
150 dev_err(&client->dev, "Read from device fails\n");
151 return ret;
152 }
153
154 return 0;
155}
156
157/*
158 * Perform some start-of-day setup, including reading the asa calibration
159 * values and caching them.
160 */
161static int ak8975_setup(struct i2c_client *client)
162{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530163 struct iio_dev *indio_dev = i2c_get_clientdata(client);
164 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700165 u8 device_id;
166 int ret;
167
168 /* Confirm that the device we're talking to is really an AK8975. */
169 ret = ak8975_read_data(client, AK8975_REG_WIA, 1, &device_id);
170 if (ret < 0) {
171 dev_err(&client->dev, "Error reading WIA\n");
172 return ret;
173 }
174 if (device_id != AK8975_DEVICE_ID) {
175 dev_err(&client->dev, "Device ak8975 not found\n");
176 return -ENODEV;
177 }
178
179 /* Write the fused rom access mode. */
180 ret = ak8975_write_data(client,
181 AK8975_REG_CNTL,
182 AK8975_REG_CNTL_MODE_FUSE_ROM,
183 AK8975_REG_CNTL_MODE_MASK,
184 AK8975_REG_CNTL_MODE_SHIFT);
185 if (ret < 0) {
186 dev_err(&client->dev, "Error in setting fuse access mode\n");
187 return ret;
188 }
189
190 /* Get asa data and store in the device data. */
191 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
192 if (ret < 0) {
193 dev_err(&client->dev, "Not able to read asa data\n");
194 return ret;
195 }
196
Andrew Chew3285aae2010-09-08 22:02:17 -0700197/*
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100198 * Precalculate scale factor (in Gauss units) for each axis and
199 * store in the device data.
Andrew Chew3285aae2010-09-08 22:02:17 -0700200 *
201 * This scale factor is axis-dependent, and is derived from 3 calibration
202 * factors ASA(x), ASA(y), and ASA(z).
203 *
204 * These ASA values are read from the sensor device at start of day, and
205 * cached in the device context struct.
206 *
207 * Adjusting the flux value with the sensitivity adjustment value should be
208 * done via the following formula:
209 *
210 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
211 *
212 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
213 * is the resultant adjusted value.
214 *
215 * We reduce the formula to:
216 *
217 * Hadj = H * (ASA + 128) / 256
218 *
219 * H is in the range of -4096 to 4095. The magnetometer has a range of
220 * +-1229uT. To go from the raw value to uT is:
221 *
222 * HuT = H * 1229/4096, or roughly, 3/10.
223 *
224 * Since 1uT = 100 gauss, our final scale factor becomes:
225 *
226 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
227 * Hadj = H * ((ASA + 128) * 30 / 256
228 *
229 * Since ASA doesn't change, we cache the resultant scale factor into the
230 * device context in ak8975_setup().
231 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100232 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
233 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
234 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
235
236 return 0;
237}
238
239/*
240 * Shows the device's mode. 0 = off, 1 = on.
241 */
242static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
243 char *buf)
Andrew Chew3285aae2010-09-08 22:02:17 -0700244{
245 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100246 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700247
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100248 return sprintf(buf, "%u\n", data->mode);
249}
250
251/*
252 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
253 * for the magn raw attributes to be available.
254 */
255static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
256 const char *buf, size_t count)
257{
258 struct iio_dev *indio_dev = dev_get_drvdata(dev);
259 struct ak8975_data *data = iio_priv(indio_dev);
260 struct i2c_client *client = data->client;
261 bool value;
262 int ret;
263
264 /* Convert mode string and do some basic sanity checking on it.
265 only 0 or 1 are valid. */
266 ret = strtobool(buf, &value);
267 if (ret < 0)
268 return ret;
269
270 mutex_lock(&data->lock);
271
272 /* Write the mode to the device. */
273 if (data->mode != value) {
274 ret = ak8975_write_data(client,
275 AK8975_REG_CNTL,
276 (u8)value,
277 AK8975_REG_CNTL_MODE_MASK,
278 AK8975_REG_CNTL_MODE_SHIFT);
279
280 if (ret < 0) {
281 dev_err(&client->dev, "Error in setting mode\n");
282 mutex_unlock(&data->lock);
283 return ret;
284 }
285 data->mode = value;
286 }
287
288 mutex_unlock(&data->lock);
289
290 return count;
Andrew Chew3285aae2010-09-08 22:02:17 -0700291}
292
Alan Cox01fbb472011-04-06 13:31:40 +0100293static int wait_conversion_complete_gpio(struct ak8975_data *data)
294{
295 struct i2c_client *client = data->client;
296 u8 read_status;
297 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
298 int ret;
299
300 /* Wait for the conversion to complete. */
301 while (timeout_ms) {
302 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
303 if (gpio_get_value(data->eoc_gpio))
304 break;
305 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
306 }
307 if (!timeout_ms) {
308 dev_err(&client->dev, "Conversion timeout happened\n");
309 return -EINVAL;
310 }
311
312 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
313 if (ret < 0) {
314 dev_err(&client->dev, "Error in reading ST1\n");
315 return ret;
316 }
317 return read_status;
318}
319
320static int wait_conversion_complete_polled(struct ak8975_data *data)
321{
322 struct i2c_client *client = data->client;
323 u8 read_status;
324 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
325 int ret;
326
327 /* Wait for the conversion to complete. */
328 while (timeout_ms) {
329 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
330 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
331 if (ret < 0) {
332 dev_err(&client->dev, "Error in reading ST1\n");
333 return ret;
334 }
335 if (read_status)
336 break;
337 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
338 }
339 if (!timeout_ms) {
340 dev_err(&client->dev, "Conversion timeout happened\n");
341 return -EINVAL;
342 }
343 return read_status;
344}
345
Andrew Chew3285aae2010-09-08 22:02:17 -0700346/*
347 * Emits the raw flux value for the x, y, or z axis.
348 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100349static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700350{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100351 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700352 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700353 u16 meas_reg;
354 s16 raw;
355 u8 read_status;
356 int ret;
357
358 mutex_lock(&data->lock);
359
360 if (data->mode == 0) {
361 dev_err(&client->dev, "Operating mode is in power down mode\n");
362 ret = -EBUSY;
363 goto exit;
364 }
365
366 /* Set up the device for taking a sample. */
367 ret = ak8975_write_data(client,
368 AK8975_REG_CNTL,
369 AK8975_REG_CNTL_MODE_ONCE,
370 AK8975_REG_CNTL_MODE_MASK,
371 AK8975_REG_CNTL_MODE_SHIFT);
372 if (ret < 0) {
373 dev_err(&client->dev, "Error in setting operating mode\n");
374 goto exit;
375 }
376
377 /* Wait for the conversion to complete. */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100378 if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100379 ret = wait_conversion_complete_gpio(data);
380 else
381 ret = wait_conversion_complete_polled(data);
382 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700383 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700384
Alan Cox01fbb472011-04-06 13:31:40 +0100385 read_status = ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700386
387 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
388 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
389 if (ret < 0) {
390 dev_err(&client->dev, "Error in reading ST2\n");
391 goto exit;
392 }
393 if (read_status & (AK8975_REG_ST2_DERR_MASK |
394 AK8975_REG_ST2_HOFL_MASK)) {
395 dev_err(&client->dev, "ST2 status error 0x%x\n",
396 read_status);
397 ret = -EINVAL;
398 goto exit;
399 }
400 }
401
402 /* Read the flux value from the appropriate register
403 (the register is specified in the iio device attributes). */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100404 ret = ak8975_read_data(client, ak8975_index_to_reg[index],
405 2, (u8 *)&meas_reg);
Andrew Chew3285aae2010-09-08 22:02:17 -0700406 if (ret < 0) {
407 dev_err(&client->dev, "Read axis data fails\n");
408 goto exit;
409 }
410
411 mutex_unlock(&data->lock);
412
413 /* Endian conversion of the measured values. */
414 raw = (s16) (le16_to_cpu(meas_reg));
415
416 /* Clamp to valid range. */
417 raw = clamp_t(s16, raw, -4096, 4095);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100418 *val = raw;
419 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700420
421exit:
422 mutex_unlock(&data->lock);
423 return ret;
424}
425
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100426static int ak8975_read_raw(struct iio_dev *indio_dev,
427 struct iio_chan_spec const *chan,
428 int *val, int *val2,
429 long mask)
430{
431 struct ak8975_data *data = iio_priv(indio_dev);
432
433 switch (mask) {
434 case 0:
435 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100436 case IIO_CHAN_INFO_SCALE:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100437 *val = data->raw_to_gauss[chan->address];
438 return IIO_VAL_INT;
439 }
440 return -EINVAL;
441}
442
443#define AK8975_CHANNEL(axis, index) \
444 { \
445 .type = IIO_MAGN, \
446 .modified = 1, \
447 .channel2 = IIO_MOD_##axis, \
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100448 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100449 .address = index, \
450 }
451
452static const struct iio_chan_spec ak8975_channels[] = {
453 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
454};
455
Andrew Chew3285aae2010-09-08 22:02:17 -0700456static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode, store_mode, 0);
Andrew Chew3285aae2010-09-08 22:02:17 -0700457
458static struct attribute *ak8975_attr[] = {
459 &iio_dev_attr_mode.dev_attr.attr,
Andrew Chew3285aae2010-09-08 22:02:17 -0700460 NULL
461};
462
463static struct attribute_group ak8975_attr_group = {
464 .attrs = ak8975_attr,
465};
466
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100467static const struct iio_info ak8975_info = {
468 .attrs = &ak8975_attr_group,
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100469 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100470 .driver_module = THIS_MODULE,
471};
472
Andrew Chew3285aae2010-09-08 22:02:17 -0700473static int ak8975_probe(struct i2c_client *client,
474 const struct i2c_device_id *id)
475{
476 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100477 struct iio_dev *indio_dev;
478 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700479 int err;
480
Andrew Chew3285aae2010-09-08 22:02:17 -0700481 /* Grab and set up the supplied GPIO. */
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100482 if (client->dev.platform_data == NULL)
483 eoc_gpio = -1;
484 else
485 eoc_gpio = *(int *)(client->dev.platform_data);
Andrew Chew3285aae2010-09-08 22:02:17 -0700486
Alan Cox01fbb472011-04-06 13:31:40 +0100487 /* We may not have a GPIO based IRQ to scan, that is fine, we will
488 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100489 if (gpio_is_valid(eoc_gpio)) {
Jonathan Cameron338473c2011-06-27 13:07:54 +0100490 err = gpio_request(eoc_gpio, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100491 if (err < 0) {
492 dev_err(&client->dev,
493 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100494 eoc_gpio, err);
495 goto exit;
Alan Cox01fbb472011-04-06 13:31:40 +0100496 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700497
Jonathan Cameron338473c2011-06-27 13:07:54 +0100498 err = gpio_direction_input(eoc_gpio);
Alan Cox01fbb472011-04-06 13:31:40 +0100499 if (err < 0) {
500 dev_err(&client->dev,
501 "Failed to configure input direction for GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100502 eoc_gpio, err);
Alan Cox01fbb472011-04-06 13:31:40 +0100503 goto exit_gpio;
504 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100505 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700506
Jonathan Cameron338473c2011-06-27 13:07:54 +0100507 /* Register with IIO */
508 indio_dev = iio_allocate_device(sizeof(*data));
509 if (indio_dev == NULL) {
510 err = -ENOMEM;
511 goto exit_gpio;
512 }
513 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530514 i2c_set_clientdata(client, indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700515 /* Perform some basic start-of-day setup of the device. */
516 err = ak8975_setup(client);
517 if (err < 0) {
518 dev_err(&client->dev, "AK8975 initialization fails\n");
Stephen Warrenad31d252011-09-21 11:16:01 +0100519 goto exit_free_iio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700520 }
521
Jonathan Cameron338473c2011-06-27 13:07:54 +0100522 data->client = client;
523 mutex_init(&data->lock);
524 data->eoc_irq = client->irq;
525 data->eoc_gpio = eoc_gpio;
526 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100527 indio_dev->channels = ak8975_channels;
528 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100529 indio_dev->info = &ak8975_info;
530 indio_dev->modes = INDIO_DIRECT_MODE;
Andrew Chew3285aae2010-09-08 22:02:17 -0700531
Jonathan Cameron338473c2011-06-27 13:07:54 +0100532 err = iio_device_register(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700533 if (err < 0)
534 goto exit_free_iio;
535
536 return 0;
537
538exit_free_iio:
Jonathan Cameron338473c2011-06-27 13:07:54 +0100539 iio_free_device(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700540exit_gpio:
Stephen Warren7c6c9362011-09-21 11:16:00 +0100541 if (gpio_is_valid(eoc_gpio))
Jonathan Cameron338473c2011-06-27 13:07:54 +0100542 gpio_free(eoc_gpio);
Andrew Chew3285aae2010-09-08 22:02:17 -0700543exit:
544 return err;
545}
546
547static int ak8975_remove(struct i2c_client *client)
548{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100549 struct iio_dev *indio_dev = i2c_get_clientdata(client);
550 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700551
Jonathan Cameron338473c2011-06-27 13:07:54 +0100552 iio_device_unregister(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700553
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100554 if (gpio_is_valid(data->eoc_gpio))
555 gpio_free(data->eoc_gpio);
556
557 iio_free_device(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700558
559 return 0;
560}
561
562static const struct i2c_device_id ak8975_id[] = {
563 {"ak8975", 0},
564 {}
565};
566
567MODULE_DEVICE_TABLE(i2c, ak8975_id);
568
Olof Johansson54461c32011-12-22 18:46:42 -0800569static const struct of_device_id ak8975_of_match[] = {
570 { .compatible = "asahi-kasei,ak8975", },
571 { .compatible = "ak8975", },
572 { }
573};
574MODULE_DEVICE_TABLE(of, ak8975_of_match);
575
Andrew Chew3285aae2010-09-08 22:02:17 -0700576static struct i2c_driver ak8975_driver = {
577 .driver = {
578 .name = "ak8975",
Olof Johansson54461c32011-12-22 18:46:42 -0800579 .of_match_table = ak8975_of_match,
Andrew Chew3285aae2010-09-08 22:02:17 -0700580 },
581 .probe = ak8975_probe,
582 .remove = __devexit_p(ak8975_remove),
583 .id_table = ak8975_id,
584};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100585module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700586
587MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
588MODULE_DESCRIPTION("AK8975 magnetometer driver");
589MODULE_LICENSE("GPL");