blob: 11bde3fd93b5e2d182e10755e466909faa69ce25 [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
Jonathan Cameron06458e22012-04-25 15:54:58 +010033#include <linux/iio/iio.h>
34#include <linux/iio/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
Leed Aguilar040f3e52012-06-06 16:14:56 -0400197 /* After reading fuse ROM data set power-down mode */
198 ret = ak8975_write_data(client,
199 AK8975_REG_CNTL,
200 AK8975_REG_CNTL_MODE_POWER_DOWN,
201 AK8975_REG_CNTL_MODE_MASK,
202 AK8975_REG_CNTL_MODE_SHIFT);
203 if (ret < 0) {
204 dev_err(&client->dev, "Error in setting power-down mode\n");
205 return ret;
206 }
207
Andrew Chew3285aae2010-09-08 22:02:17 -0700208/*
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100209 * Precalculate scale factor (in Gauss units) for each axis and
210 * store in the device data.
Andrew Chew3285aae2010-09-08 22:02:17 -0700211 *
212 * This scale factor is axis-dependent, and is derived from 3 calibration
213 * factors ASA(x), ASA(y), and ASA(z).
214 *
215 * These ASA values are read from the sensor device at start of day, and
216 * cached in the device context struct.
217 *
218 * Adjusting the flux value with the sensitivity adjustment value should be
219 * done via the following formula:
220 *
221 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
222 *
223 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
224 * is the resultant adjusted value.
225 *
226 * We reduce the formula to:
227 *
228 * Hadj = H * (ASA + 128) / 256
229 *
230 * H is in the range of -4096 to 4095. The magnetometer has a range of
231 * +-1229uT. To go from the raw value to uT is:
232 *
233 * HuT = H * 1229/4096, or roughly, 3/10.
234 *
235 * Since 1uT = 100 gauss, our final scale factor becomes:
236 *
237 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
238 * Hadj = H * ((ASA + 128) * 30 / 256
239 *
240 * Since ASA doesn't change, we cache the resultant scale factor into the
241 * device context in ak8975_setup().
242 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100243 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
244 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
245 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
246
247 return 0;
248}
249
250/*
251 * Shows the device's mode. 0 = off, 1 = on.
252 */
253static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
254 char *buf)
Andrew Chew3285aae2010-09-08 22:02:17 -0700255{
Lars-Peter Clausen527cf712012-05-12 15:39:52 +0200256 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100257 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700258
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100259 return sprintf(buf, "%u\n", data->mode);
260}
261
262/*
263 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
264 * for the magn raw attributes to be available.
265 */
266static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
267 const char *buf, size_t count)
268{
Lars-Peter Clausen527cf712012-05-12 15:39:52 +0200269 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100270 struct ak8975_data *data = iio_priv(indio_dev);
271 struct i2c_client *client = data->client;
272 bool value;
273 int ret;
274
275 /* Convert mode string and do some basic sanity checking on it.
276 only 0 or 1 are valid. */
277 ret = strtobool(buf, &value);
278 if (ret < 0)
279 return ret;
280
281 mutex_lock(&data->lock);
282
283 /* Write the mode to the device. */
284 if (data->mode != value) {
285 ret = ak8975_write_data(client,
286 AK8975_REG_CNTL,
287 (u8)value,
288 AK8975_REG_CNTL_MODE_MASK,
289 AK8975_REG_CNTL_MODE_SHIFT);
290
291 if (ret < 0) {
292 dev_err(&client->dev, "Error in setting mode\n");
293 mutex_unlock(&data->lock);
294 return ret;
295 }
296 data->mode = value;
297 }
298
299 mutex_unlock(&data->lock);
300
301 return count;
Andrew Chew3285aae2010-09-08 22:02:17 -0700302}
303
Alan Cox01fbb472011-04-06 13:31:40 +0100304static int wait_conversion_complete_gpio(struct ak8975_data *data)
305{
306 struct i2c_client *client = data->client;
307 u8 read_status;
308 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
309 int ret;
310
311 /* Wait for the conversion to complete. */
312 while (timeout_ms) {
313 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
314 if (gpio_get_value(data->eoc_gpio))
315 break;
316 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
317 }
318 if (!timeout_ms) {
319 dev_err(&client->dev, "Conversion timeout happened\n");
320 return -EINVAL;
321 }
322
323 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
324 if (ret < 0) {
325 dev_err(&client->dev, "Error in reading ST1\n");
326 return ret;
327 }
328 return read_status;
329}
330
331static int wait_conversion_complete_polled(struct ak8975_data *data)
332{
333 struct i2c_client *client = data->client;
334 u8 read_status;
335 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
336 int ret;
337
338 /* Wait for the conversion to complete. */
339 while (timeout_ms) {
340 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
341 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
342 if (ret < 0) {
343 dev_err(&client->dev, "Error in reading ST1\n");
344 return ret;
345 }
346 if (read_status)
347 break;
348 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
349 }
350 if (!timeout_ms) {
351 dev_err(&client->dev, "Conversion timeout happened\n");
352 return -EINVAL;
353 }
354 return read_status;
355}
356
Andrew Chew3285aae2010-09-08 22:02:17 -0700357/*
358 * Emits the raw flux value for the x, y, or z axis.
359 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100360static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700361{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100362 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700363 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700364 u16 meas_reg;
365 s16 raw;
366 u8 read_status;
367 int ret;
368
369 mutex_lock(&data->lock);
370
371 if (data->mode == 0) {
372 dev_err(&client->dev, "Operating mode is in power down mode\n");
373 ret = -EBUSY;
374 goto exit;
375 }
376
377 /* Set up the device for taking a sample. */
378 ret = ak8975_write_data(client,
379 AK8975_REG_CNTL,
380 AK8975_REG_CNTL_MODE_ONCE,
381 AK8975_REG_CNTL_MODE_MASK,
382 AK8975_REG_CNTL_MODE_SHIFT);
383 if (ret < 0) {
384 dev_err(&client->dev, "Error in setting operating mode\n");
385 goto exit;
386 }
387
388 /* Wait for the conversion to complete. */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100389 if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100390 ret = wait_conversion_complete_gpio(data);
391 else
392 ret = wait_conversion_complete_polled(data);
393 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700394 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700395
Alan Cox01fbb472011-04-06 13:31:40 +0100396 read_status = ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700397
398 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
399 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
400 if (ret < 0) {
401 dev_err(&client->dev, "Error in reading ST2\n");
402 goto exit;
403 }
404 if (read_status & (AK8975_REG_ST2_DERR_MASK |
405 AK8975_REG_ST2_HOFL_MASK)) {
406 dev_err(&client->dev, "ST2 status error 0x%x\n",
407 read_status);
408 ret = -EINVAL;
409 goto exit;
410 }
411 }
412
413 /* Read the flux value from the appropriate register
414 (the register is specified in the iio device attributes). */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100415 ret = ak8975_read_data(client, ak8975_index_to_reg[index],
416 2, (u8 *)&meas_reg);
Andrew Chew3285aae2010-09-08 22:02:17 -0700417 if (ret < 0) {
418 dev_err(&client->dev, "Read axis data fails\n");
419 goto exit;
420 }
421
422 mutex_unlock(&data->lock);
423
424 /* Endian conversion of the measured values. */
425 raw = (s16) (le16_to_cpu(meas_reg));
426
427 /* Clamp to valid range. */
428 raw = clamp_t(s16, raw, -4096, 4095);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100429 *val = raw;
430 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700431
432exit:
433 mutex_unlock(&data->lock);
434 return ret;
435}
436
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100437static int ak8975_read_raw(struct iio_dev *indio_dev,
438 struct iio_chan_spec const *chan,
439 int *val, int *val2,
440 long mask)
441{
442 struct ak8975_data *data = iio_priv(indio_dev);
443
444 switch (mask) {
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100445 case IIO_CHAN_INFO_RAW:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100446 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100447 case IIO_CHAN_INFO_SCALE:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100448 *val = data->raw_to_gauss[chan->address];
449 return IIO_VAL_INT;
450 }
451 return -EINVAL;
452}
453
454#define AK8975_CHANNEL(axis, index) \
455 { \
456 .type = IIO_MAGN, \
457 .modified = 1, \
458 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100459 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
460 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100461 .address = index, \
462 }
463
464static const struct iio_chan_spec ak8975_channels[] = {
465 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
466};
467
Andrew Chew3285aae2010-09-08 22:02:17 -0700468static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode, store_mode, 0);
Andrew Chew3285aae2010-09-08 22:02:17 -0700469
470static struct attribute *ak8975_attr[] = {
471 &iio_dev_attr_mode.dev_attr.attr,
Andrew Chew3285aae2010-09-08 22:02:17 -0700472 NULL
473};
474
475static struct attribute_group ak8975_attr_group = {
476 .attrs = ak8975_attr,
477};
478
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100479static const struct iio_info ak8975_info = {
480 .attrs = &ak8975_attr_group,
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100481 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100482 .driver_module = THIS_MODULE,
483};
484
Shubhrajyoti Df61343b2012-06-04 15:11:05 +0530485static int __devinit ak8975_probe(struct i2c_client *client,
Andrew Chew3285aae2010-09-08 22:02:17 -0700486 const struct i2c_device_id *id)
487{
488 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100489 struct iio_dev *indio_dev;
490 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700491 int err;
492
Andrew Chew3285aae2010-09-08 22:02:17 -0700493 /* Grab and set up the supplied GPIO. */
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100494 if (client->dev.platform_data == NULL)
495 eoc_gpio = -1;
496 else
497 eoc_gpio = *(int *)(client->dev.platform_data);
Andrew Chew3285aae2010-09-08 22:02:17 -0700498
Alan Cox01fbb472011-04-06 13:31:40 +0100499 /* We may not have a GPIO based IRQ to scan, that is fine, we will
500 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100501 if (gpio_is_valid(eoc_gpio)) {
Leed Aguilar82f2acd2012-06-06 16:15:40 -0400502 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100503 if (err < 0) {
504 dev_err(&client->dev,
505 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100506 eoc_gpio, err);
507 goto exit;
Alan Cox01fbb472011-04-06 13:31:40 +0100508 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100509 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700510
Jonathan Cameron338473c2011-06-27 13:07:54 +0100511 /* Register with IIO */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200512 indio_dev = iio_device_alloc(sizeof(*data));
Jonathan Cameron338473c2011-06-27 13:07:54 +0100513 if (indio_dev == NULL) {
514 err = -ENOMEM;
515 goto exit_gpio;
516 }
517 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530518 i2c_set_clientdata(client, indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700519 /* Perform some basic start-of-day setup of the device. */
520 err = ak8975_setup(client);
521 if (err < 0) {
522 dev_err(&client->dev, "AK8975 initialization fails\n");
Stephen Warrenad31d252011-09-21 11:16:01 +0100523 goto exit_free_iio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700524 }
525
Jonathan Cameron338473c2011-06-27 13:07:54 +0100526 data->client = client;
527 mutex_init(&data->lock);
528 data->eoc_irq = client->irq;
529 data->eoc_gpio = eoc_gpio;
530 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100531 indio_dev->channels = ak8975_channels;
532 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100533 indio_dev->info = &ak8975_info;
534 indio_dev->modes = INDIO_DIRECT_MODE;
Andrew Chew3285aae2010-09-08 22:02:17 -0700535
Jonathan Cameron338473c2011-06-27 13:07:54 +0100536 err = iio_device_register(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700537 if (err < 0)
538 goto exit_free_iio;
539
540 return 0;
541
542exit_free_iio:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200543 iio_device_free(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700544exit_gpio:
Stephen Warren7c6c9362011-09-21 11:16:00 +0100545 if (gpio_is_valid(eoc_gpio))
Jonathan Cameron338473c2011-06-27 13:07:54 +0100546 gpio_free(eoc_gpio);
Andrew Chew3285aae2010-09-08 22:02:17 -0700547exit:
548 return err;
549}
550
Shubhrajyoti Df61343b2012-06-04 15:11:05 +0530551static int __devexit ak8975_remove(struct i2c_client *client)
Andrew Chew3285aae2010-09-08 22:02:17 -0700552{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100553 struct iio_dev *indio_dev = i2c_get_clientdata(client);
554 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700555
Jonathan Cameron338473c2011-06-27 13:07:54 +0100556 iio_device_unregister(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700557
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100558 if (gpio_is_valid(data->eoc_gpio))
559 gpio_free(data->eoc_gpio);
560
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200561 iio_device_free(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700562
563 return 0;
564}
565
566static const struct i2c_device_id ak8975_id[] = {
567 {"ak8975", 0},
568 {}
569};
570
571MODULE_DEVICE_TABLE(i2c, ak8975_id);
572
Olof Johansson54461c32011-12-22 18:46:42 -0800573static const struct of_device_id ak8975_of_match[] = {
574 { .compatible = "asahi-kasei,ak8975", },
575 { .compatible = "ak8975", },
576 { }
577};
578MODULE_DEVICE_TABLE(of, ak8975_of_match);
579
Andrew Chew3285aae2010-09-08 22:02:17 -0700580static struct i2c_driver ak8975_driver = {
581 .driver = {
582 .name = "ak8975",
Olof Johansson54461c32011-12-22 18:46:42 -0800583 .of_match_table = ak8975_of_match,
Andrew Chew3285aae2010-09-08 22:02:17 -0700584 },
585 .probe = ak8975_probe,
586 .remove = __devexit_p(ak8975_remove),
587 .id_table = ak8975_id,
588};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100589module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700590
591MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
592MODULE_DESCRIPTION("AK8975 magnetometer driver");
593MODULE_LICENSE("GPL");