Commit | Line | Data |
---|---|---|
3285aae1 AC |
1 | /* |
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> | |
94a6d5cf | 27 | #include <linux/interrupt.h> |
3285aae1 AC |
28 | #include <linux/err.h> |
29 | #include <linux/mutex.h> | |
30 | #include <linux/delay.h> | |
94a6d5cf | 31 | #include <linux/bitops.h> |
3285aae1 | 32 | #include <linux/gpio.h> |
f4b7f751 | 33 | #include <linux/of_gpio.h> |
3285aae1 | 34 | |
06458e27 JC |
35 | #include <linux/iio/iio.h> |
36 | #include <linux/iio/sysfs.h> | |
3285aae1 AC |
37 | /* |
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 | |
94a6d5cf | 87 | #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) |
6027c077 SP |
88 | #define RAW_TO_GAUSS_8975(asa) ((((asa) + 128) * 3000) / 256) |
89 | #define RAW_TO_GAUSS_8963(asa) ((((asa) + 128) * 6000) / 256) | |
90 | ||
91 | /* Compatible Asahi Kasei Compass parts */ | |
92 | enum asahi_compass_chipset { | |
93 | AK8975, | |
94 | AK8963, | |
95 | }; | |
3285aae1 AC |
96 | |
97 | /* | |
98 | * Per-instance context data for the device. | |
99 | */ | |
100 | struct ak8975_data { | |
101 | struct i2c_client *client; | |
3285aae1 AC |
102 | struct attribute_group attrs; |
103 | struct mutex lock; | |
104 | u8 asa[3]; | |
105 | long raw_to_gauss[3]; | |
3285aae1 AC |
106 | u8 reg_cache[AK8975_MAX_REGS]; |
107 | int eoc_gpio; | |
94a6d5cf JA |
108 | int eoc_irq; |
109 | wait_queue_head_t data_ready_queue; | |
110 | unsigned long flags; | |
6027c077 | 111 | enum asahi_compass_chipset chipset; |
3285aae1 AC |
112 | }; |
113 | ||
694e1b5f JC |
114 | static const int ak8975_index_to_reg[] = { |
115 | AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL, | |
116 | }; | |
117 | ||
3285aae1 AC |
118 | /* |
119 | * Helper function to write to the I2C device's registers. | |
120 | */ | |
121 | static int ak8975_write_data(struct i2c_client *client, | |
122 | u8 reg, u8 val, u8 mask, u8 shift) | |
123 | { | |
40f32d93 PC |
124 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
125 | struct ak8975_data *data = iio_priv(indio_dev); | |
694e1b5f JC |
126 | u8 regval; |
127 | int ret; | |
3285aae1 | 128 | |
694e1b5f JC |
129 | regval = (data->reg_cache[reg] & ~mask) | (val << shift); |
130 | ret = i2c_smbus_write_byte_data(client, reg, regval); | |
3285aae1 AC |
131 | if (ret < 0) { |
132 | dev_err(&client->dev, "Write to device fails status %x\n", ret); | |
133 | return ret; | |
134 | } | |
135 | data->reg_cache[reg] = regval; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
94a6d5cf JA |
140 | /* |
141 | * Handle data ready irq | |
142 | */ | |
143 | static irqreturn_t ak8975_irq_handler(int irq, void *data) | |
144 | { | |
145 | struct ak8975_data *ak8975 = data; | |
146 | ||
147 | set_bit(0, &ak8975->flags); | |
148 | wake_up(&ak8975->data_ready_queue); | |
149 | ||
150 | return IRQ_HANDLED; | |
151 | } | |
152 | ||
153 | /* | |
154 | * Install data ready interrupt handler | |
155 | */ | |
156 | static int ak8975_setup_irq(struct ak8975_data *data) | |
157 | { | |
158 | struct i2c_client *client = data->client; | |
159 | int rc; | |
160 | int irq; | |
161 | ||
162 | if (client->irq) | |
163 | irq = client->irq; | |
164 | else | |
165 | irq = gpio_to_irq(data->eoc_gpio); | |
166 | ||
167 | rc = request_irq(irq, ak8975_irq_handler, | |
168 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
169 | dev_name(&client->dev), data); | |
170 | if (rc < 0) { | |
171 | dev_err(&client->dev, | |
172 | "irq %d request failed, (gpio %d): %d\n", | |
173 | irq, data->eoc_gpio, rc); | |
174 | return rc; | |
175 | } | |
176 | ||
177 | init_waitqueue_head(&data->data_ready_queue); | |
178 | clear_bit(0, &data->flags); | |
179 | data->eoc_irq = irq; | |
180 | ||
181 | return rc; | |
182 | } | |
183 | ||
184 | ||
3285aae1 AC |
185 | /* |
186 | * Perform some start-of-day setup, including reading the asa calibration | |
187 | * values and caching them. | |
188 | */ | |
189 | static int ak8975_setup(struct i2c_client *client) | |
190 | { | |
40f32d93 PC |
191 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
192 | struct ak8975_data *data = iio_priv(indio_dev); | |
3285aae1 AC |
193 | u8 device_id; |
194 | int ret; | |
195 | ||
196 | /* Confirm that the device we're talking to is really an AK8975. */ | |
c411f600 | 197 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA); |
3285aae1 AC |
198 | if (ret < 0) { |
199 | dev_err(&client->dev, "Error reading WIA\n"); | |
200 | return ret; | |
201 | } | |
c411f600 | 202 | device_id = ret; |
3285aae1 AC |
203 | if (device_id != AK8975_DEVICE_ID) { |
204 | dev_err(&client->dev, "Device ak8975 not found\n"); | |
205 | return -ENODEV; | |
206 | } | |
207 | ||
208 | /* Write the fused rom access mode. */ | |
209 | ret = ak8975_write_data(client, | |
210 | AK8975_REG_CNTL, | |
211 | AK8975_REG_CNTL_MODE_FUSE_ROM, | |
212 | AK8975_REG_CNTL_MODE_MASK, | |
213 | AK8975_REG_CNTL_MODE_SHIFT); | |
214 | if (ret < 0) { | |
215 | dev_err(&client->dev, "Error in setting fuse access mode\n"); | |
216 | return ret; | |
217 | } | |
218 | ||
219 | /* Get asa data and store in the device data. */ | |
c411f600 JC |
220 | ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX, |
221 | 3, data->asa); | |
3285aae1 AC |
222 | if (ret < 0) { |
223 | dev_err(&client->dev, "Not able to read asa data\n"); | |
224 | return ret; | |
225 | } | |
226 | ||
040f3e58 LA |
227 | /* After reading fuse ROM data set power-down mode */ |
228 | ret = ak8975_write_data(client, | |
229 | AK8975_REG_CNTL, | |
230 | AK8975_REG_CNTL_MODE_POWER_DOWN, | |
231 | AK8975_REG_CNTL_MODE_MASK, | |
232 | AK8975_REG_CNTL_MODE_SHIFT); | |
94a6d5cf JA |
233 | |
234 | if (data->eoc_gpio > 0 || client->irq) { | |
235 | ret = ak8975_setup_irq(data); | |
236 | if (ret < 0) { | |
237 | dev_err(&client->dev, | |
238 | "Error setting data ready interrupt\n"); | |
239 | return ret; | |
240 | } | |
241 | } | |
242 | ||
040f3e58 LA |
243 | if (ret < 0) { |
244 | dev_err(&client->dev, "Error in setting power-down mode\n"); | |
245 | return ret; | |
246 | } | |
247 | ||
694e1b5f JC |
248 | /* |
249 | * Precalculate scale factor (in Gauss units) for each axis and | |
250 | * store in the device data. | |
251 | * | |
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 | * | |
eb03610a | 275 | * Since 1uT = 0.01 gauss, our final scale factor becomes: |
694e1b5f | 276 | * |
bef44abc BS |
277 | * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 |
278 | * Hadj = H * ((ASA + 128) * 0.003) / 256 | |
694e1b5f JC |
279 | * |
280 | * Since ASA doesn't change, we cache the resultant scale factor into the | |
281 | * device context in ak8975_setup(). | |
282 | */ | |
6027c077 SP |
283 | 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 | } | |
3285aae1 AC |
298 | |
299 | return 0; | |
300 | } | |
301 | ||
01fbb478 AC |
302 | static int wait_conversion_complete_gpio(struct ak8975_data *data) |
303 | { | |
304 | struct i2c_client *client = data->client; | |
01fbb478 AC |
305 | 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 | ||
c411f600 JC |
320 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
321 | if (ret < 0) | |
01fbb478 | 322 | dev_err(&client->dev, "Error in reading ST1\n"); |
c411f600 JC |
323 | |
324 | return ret; | |
01fbb478 AC |
325 | } |
326 | ||
327 | static 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); | |
c411f600 | 337 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
01fbb478 AC |
338 | if (ret < 0) { |
339 | dev_err(&client->dev, "Error in reading ST1\n"); | |
340 | return ret; | |
341 | } | |
c411f600 | 342 | read_status = ret; |
01fbb478 AC |
343 | 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 | } | |
94a6d5cf | 351 | |
01fbb478 AC |
352 | return read_status; |
353 | } | |
354 | ||
94a6d5cf JA |
355 | /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ |
356 | static 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 | ||
3285aae1 AC |
368 | /* |
369 | * Emits the raw flux value for the x, y, or z axis. | |
370 | */ | |
694e1b5f | 371 | static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) |
3285aae1 | 372 | { |
338473c8 | 373 | struct ak8975_data *data = iio_priv(indio_dev); |
3285aae1 | 374 | struct i2c_client *client = data->client; |
3285aae1 AC |
375 | u16 meas_reg; |
376 | s16 raw; | |
3285aae1 AC |
377 | int ret; |
378 | ||
379 | mutex_lock(&data->lock); | |
380 | ||
3285aae1 AC |
381 | /* Set up the device for taking a sample. */ |
382 | ret = ak8975_write_data(client, | |
383 | AK8975_REG_CNTL, | |
384 | AK8975_REG_CNTL_MODE_ONCE, | |
385 | AK8975_REG_CNTL_MODE_MASK, | |
386 | AK8975_REG_CNTL_MODE_SHIFT); | |
387 | if (ret < 0) { | |
388 | dev_err(&client->dev, "Error in setting operating mode\n"); | |
389 | goto exit; | |
390 | } | |
391 | ||
392 | /* Wait for the conversion to complete. */ | |
94a6d5cf JA |
393 | if (data->eoc_irq) |
394 | ret = wait_conversion_complete_interrupt(data); | |
395 | else if (gpio_is_valid(data->eoc_gpio)) | |
01fbb478 AC |
396 | ret = wait_conversion_complete_gpio(data); |
397 | else | |
398 | ret = wait_conversion_complete_polled(data); | |
399 | if (ret < 0) | |
3285aae1 | 400 | goto exit; |
3285aae1 | 401 | |
94a6d5cf | 402 | /* This will be executed only for non-interrupt based waiting case */ |
c411f600 JC |
403 | if (ret & AK8975_REG_ST1_DRDY_MASK) { |
404 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2); | |
3285aae1 AC |
405 | if (ret < 0) { |
406 | dev_err(&client->dev, "Error in reading ST2\n"); | |
407 | goto exit; | |
408 | } | |
c411f600 JC |
409 | if (ret & (AK8975_REG_ST2_DERR_MASK | |
410 | AK8975_REG_ST2_HOFL_MASK)) { | |
411 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); | |
3285aae1 AC |
412 | ret = -EINVAL; |
413 | goto exit; | |
414 | } | |
415 | } | |
416 | ||
417 | /* Read the flux value from the appropriate register | |
418 | (the register is specified in the iio device attributes). */ | |
c411f600 | 419 | ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]); |
3285aae1 AC |
420 | if (ret < 0) { |
421 | dev_err(&client->dev, "Read axis data fails\n"); | |
422 | goto exit; | |
423 | } | |
c411f600 | 424 | meas_reg = ret; |
3285aae1 AC |
425 | |
426 | mutex_unlock(&data->lock); | |
427 | ||
428 | /* Endian conversion of the measured values. */ | |
429 | raw = (s16) (le16_to_cpu(meas_reg)); | |
430 | ||
431 | /* Clamp to valid range. */ | |
432 | raw = clamp_t(s16, raw, -4096, 4095); | |
694e1b5f JC |
433 | *val = raw; |
434 | return IIO_VAL_INT; | |
3285aae1 AC |
435 | |
436 | exit: | |
437 | mutex_unlock(&data->lock); | |
438 | return ret; | |
439 | } | |
440 | ||
694e1b5f JC |
441 | static int ak8975_read_raw(struct iio_dev *indio_dev, |
442 | struct iio_chan_spec const *chan, | |
443 | int *val, int *val2, | |
444 | long mask) | |
445 | { | |
446 | struct ak8975_data *data = iio_priv(indio_dev); | |
447 | ||
448 | switch (mask) { | |
4d9948b3 | 449 | case IIO_CHAN_INFO_RAW: |
694e1b5f | 450 | return ak8975_read_axis(indio_dev, chan->address, val); |
c8a9f805 | 451 | case IIO_CHAN_INFO_SCALE: |
bef44abc BS |
452 | *val = 0; |
453 | *val2 = data->raw_to_gauss[chan->address]; | |
454 | return IIO_VAL_INT_PLUS_MICRO; | |
694e1b5f JC |
455 | } |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | #define AK8975_CHANNEL(axis, index) \ | |
460 | { \ | |
461 | .type = IIO_MAGN, \ | |
462 | .modified = 1, \ | |
463 | .channel2 = IIO_MOD_##axis, \ | |
3a0b4422 JC |
464 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
465 | BIT(IIO_CHAN_INFO_SCALE), \ | |
694e1b5f JC |
466 | .address = index, \ |
467 | } | |
468 | ||
469 | static const struct iio_chan_spec ak8975_channels[] = { | |
470 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), | |
471 | }; | |
472 | ||
6fe8135f | 473 | static const struct iio_info ak8975_info = { |
694e1b5f | 474 | .read_raw = &ak8975_read_raw, |
6fe8135f JC |
475 | .driver_module = THIS_MODULE, |
476 | }; | |
477 | ||
4ae1c61f | 478 | static int ak8975_probe(struct i2c_client *client, |
3285aae1 AC |
479 | const struct i2c_device_id *id) |
480 | { | |
481 | struct ak8975_data *data; | |
338473c8 JC |
482 | struct iio_dev *indio_dev; |
483 | int eoc_gpio; | |
3285aae1 AC |
484 | int err; |
485 | ||
3285aae1 | 486 | /* Grab and set up the supplied GPIO. */ |
f4b7f751 | 487 | if (client->dev.platform_data) |
f6d838d7 | 488 | eoc_gpio = *(int *)(client->dev.platform_data); |
f4b7f751 JA |
489 | else if (client->dev.of_node) |
490 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); | |
491 | else | |
492 | eoc_gpio = -1; | |
493 | ||
494 | if (eoc_gpio == -EPROBE_DEFER) | |
495 | return -EPROBE_DEFER; | |
3285aae1 | 496 | |
01fbb478 AC |
497 | /* We may not have a GPIO based IRQ to scan, that is fine, we will |
498 | poll if so */ | |
7c6c9368 | 499 | if (gpio_is_valid(eoc_gpio)) { |
82f2acdc | 500 | err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); |
01fbb478 AC |
501 | if (err < 0) { |
502 | dev_err(&client->dev, | |
503 | "failed to request GPIO %d, error %d\n", | |
338473c8 JC |
504 | eoc_gpio, err); |
505 | goto exit; | |
01fbb478 | 506 | } |
7c6c9368 | 507 | } |
3285aae1 | 508 | |
338473c8 | 509 | /* Register with IIO */ |
7cbb7537 | 510 | indio_dev = iio_device_alloc(sizeof(*data)); |
338473c8 JC |
511 | if (indio_dev == NULL) { |
512 | err = -ENOMEM; | |
513 | goto exit_gpio; | |
514 | } | |
515 | data = iio_priv(indio_dev); | |
40f32d93 | 516 | i2c_set_clientdata(client, indio_dev); |
94a6d5cf JA |
517 | |
518 | data->client = client; | |
519 | data->eoc_gpio = eoc_gpio; | |
520 | data->eoc_irq = 0; | |
521 | ||
6027c077 SP |
522 | data->chipset = (enum asahi_compass_chipset)(id->driver_data); |
523 | dev_dbg(&client->dev, "Asahi compass chip %s\n", id->name); | |
524 | ||
3285aae1 AC |
525 | /* Perform some basic start-of-day setup of the device. */ |
526 | err = ak8975_setup(client); | |
527 | if (err < 0) { | |
528 | dev_err(&client->dev, "AK8975 initialization fails\n"); | |
ad31d250 | 529 | goto exit_free_iio; |
3285aae1 AC |
530 | } |
531 | ||
338473c8 JC |
532 | data->client = client; |
533 | mutex_init(&data->lock); | |
338473c8 JC |
534 | data->eoc_gpio = eoc_gpio; |
535 | indio_dev->dev.parent = &client->dev; | |
694e1b5f JC |
536 | indio_dev->channels = ak8975_channels; |
537 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); | |
338473c8 | 538 | indio_dev->info = &ak8975_info; |
54ab3e24 | 539 | indio_dev->name = id->name; |
338473c8 | 540 | indio_dev->modes = INDIO_DIRECT_MODE; |
3285aae1 | 541 | |
338473c8 | 542 | err = iio_device_register(indio_dev); |
3285aae1 AC |
543 | if (err < 0) |
544 | goto exit_free_iio; | |
545 | ||
546 | return 0; | |
547 | ||
548 | exit_free_iio: | |
7cbb7537 | 549 | iio_device_free(indio_dev); |
94a6d5cf JA |
550 | if (data->eoc_irq) |
551 | free_irq(data->eoc_irq, data); | |
3285aae1 | 552 | exit_gpio: |
7c6c9368 | 553 | if (gpio_is_valid(eoc_gpio)) |
338473c8 | 554 | gpio_free(eoc_gpio); |
3285aae1 AC |
555 | exit: |
556 | return err; | |
557 | } | |
558 | ||
447d4f29 | 559 | static int ak8975_remove(struct i2c_client *client) |
3285aae1 | 560 | { |
338473c8 JC |
561 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
562 | struct ak8975_data *data = iio_priv(indio_dev); | |
3285aae1 | 563 | |
338473c8 | 564 | iio_device_unregister(indio_dev); |
3285aae1 | 565 | |
94a6d5cf JA |
566 | if (data->eoc_irq) |
567 | free_irq(data->eoc_irq, data); | |
568 | ||
d2fffd6c JC |
569 | if (gpio_is_valid(data->eoc_gpio)) |
570 | gpio_free(data->eoc_gpio); | |
571 | ||
7cbb7537 | 572 | iio_device_free(indio_dev); |
3285aae1 AC |
573 | |
574 | return 0; | |
575 | } | |
576 | ||
577 | static const struct i2c_device_id ak8975_id[] = { | |
6027c077 SP |
578 | {"ak8975", AK8975}, |
579 | {"ak8963", AK8963}, | |
3285aae1 AC |
580 | {} |
581 | }; | |
582 | ||
583 | MODULE_DEVICE_TABLE(i2c, ak8975_id); | |
584 | ||
54461c30 OJ |
585 | static const struct of_device_id ak8975_of_match[] = { |
586 | { .compatible = "asahi-kasei,ak8975", }, | |
587 | { .compatible = "ak8975", }, | |
588 | { } | |
589 | }; | |
590 | MODULE_DEVICE_TABLE(of, ak8975_of_match); | |
591 | ||
3285aae1 AC |
592 | static struct i2c_driver ak8975_driver = { |
593 | .driver = { | |
594 | .name = "ak8975", | |
54461c30 | 595 | .of_match_table = ak8975_of_match, |
3285aae1 AC |
596 | }, |
597 | .probe = ak8975_probe, | |
e543acf0 | 598 | .remove = ak8975_remove, |
3285aae1 AC |
599 | .id_table = ak8975_id, |
600 | }; | |
6e5af184 | 601 | module_i2c_driver(ak8975_driver); |
3285aae1 AC |
602 | |
603 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
604 | MODULE_DESCRIPTION("AK8975 magnetometer driver"); | |
605 | MODULE_LICENSE("GPL"); |