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> |
d913971e | 34 | #include <linux/acpi.h> |
3285aae1 | 35 | |
06458e27 JC |
36 | #include <linux/iio/iio.h> |
37 | #include <linux/iio/sysfs.h> | |
3285aae1 AC |
38 | /* |
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) | |
67 | #define AK8975_REG_CNTL_MODE_POWER_DOWN 0 | |
68 | #define AK8975_REG_CNTL_MODE_ONCE 1 | |
69 | #define AK8975_REG_CNTL_MODE_SELF_TEST 8 | |
70 | #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF | |
71 | ||
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 | |
94a6d5cf | 88 | #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) |
6027c077 SP |
89 | #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 */ | |
93 | enum asahi_compass_chipset { | |
94 | AK8975, | |
95 | AK8963, | |
96 | }; | |
3285aae1 AC |
97 | |
98 | /* | |
99 | * Per-instance context data for the device. | |
100 | */ | |
101 | struct ak8975_data { | |
102 | struct i2c_client *client; | |
3285aae1 AC |
103 | struct attribute_group attrs; |
104 | struct mutex lock; | |
105 | u8 asa[3]; | |
106 | long raw_to_gauss[3]; | |
3285aae1 AC |
107 | u8 reg_cache[AK8975_MAX_REGS]; |
108 | int eoc_gpio; | |
94a6d5cf JA |
109 | int eoc_irq; |
110 | wait_queue_head_t data_ready_queue; | |
111 | unsigned long flags; | |
6027c077 | 112 | enum asahi_compass_chipset chipset; |
3285aae1 AC |
113 | }; |
114 | ||
694e1b5f JC |
115 | static const int ak8975_index_to_reg[] = { |
116 | AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL, | |
117 | }; | |
118 | ||
3285aae1 AC |
119 | /* |
120 | * Helper function to write to the I2C device's registers. | |
121 | */ | |
122 | static int ak8975_write_data(struct i2c_client *client, | |
123 | u8 reg, u8 val, u8 mask, u8 shift) | |
124 | { | |
40f32d93 PC |
125 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
126 | struct ak8975_data *data = iio_priv(indio_dev); | |
694e1b5f JC |
127 | u8 regval; |
128 | int ret; | |
3285aae1 | 129 | |
694e1b5f JC |
130 | regval = (data->reg_cache[reg] & ~mask) | (val << shift); |
131 | ret = i2c_smbus_write_byte_data(client, reg, regval); | |
3285aae1 AC |
132 | 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 | ||
94a6d5cf JA |
141 | /* |
142 | * Handle data ready irq | |
143 | */ | |
144 | static 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 | */ | |
157 | static 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 | ||
168 | rc = request_irq(irq, ak8975_irq_handler, | |
169 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | |
170 | dev_name(&client->dev), data); | |
171 | 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 | ||
3285aae1 AC |
186 | /* |
187 | * Perform some start-of-day setup, including reading the asa calibration | |
188 | * values and caching them. | |
189 | */ | |
190 | static int ak8975_setup(struct i2c_client *client) | |
191 | { | |
40f32d93 PC |
192 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
193 | struct ak8975_data *data = iio_priv(indio_dev); | |
3285aae1 AC |
194 | u8 device_id; |
195 | int ret; | |
196 | ||
197 | /* Confirm that the device we're talking to is really an AK8975. */ | |
c411f600 | 198 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA); |
3285aae1 AC |
199 | if (ret < 0) { |
200 | dev_err(&client->dev, "Error reading WIA\n"); | |
201 | return ret; | |
202 | } | |
c411f600 | 203 | device_id = ret; |
3285aae1 AC |
204 | 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. */ | |
c411f600 JC |
221 | ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX, |
222 | 3, data->asa); | |
3285aae1 AC |
223 | if (ret < 0) { |
224 | dev_err(&client->dev, "Not able to read asa data\n"); | |
225 | return ret; | |
226 | } | |
227 | ||
040f3e58 LA |
228 | /* 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); | |
94a6d5cf JA |
234 | |
235 | if (data->eoc_gpio > 0 || client->irq) { | |
236 | ret = ak8975_setup_irq(data); | |
237 | if (ret < 0) { | |
238 | dev_err(&client->dev, | |
239 | "Error setting data ready interrupt\n"); | |
240 | return ret; | |
241 | } | |
242 | } | |
243 | ||
040f3e58 LA |
244 | if (ret < 0) { |
245 | dev_err(&client->dev, "Error in setting power-down mode\n"); | |
246 | return ret; | |
247 | } | |
248 | ||
694e1b5f JC |
249 | /* |
250 | * Precalculate scale factor (in Gauss units) for each axis and | |
251 | * store in the device data. | |
252 | * | |
253 | * This scale factor is axis-dependent, and is derived from 3 calibration | |
254 | * factors ASA(x), ASA(y), and ASA(z). | |
255 | * | |
256 | * These ASA values are read from the sensor device at start of day, and | |
257 | * cached in the device context struct. | |
258 | * | |
259 | * Adjusting the flux value with the sensitivity adjustment value should be | |
260 | * done via the following formula: | |
261 | * | |
262 | * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) | |
263 | * | |
264 | * where H is the raw value, ASA is the sensitivity adjustment, and Hadj | |
265 | * is the resultant adjusted value. | |
266 | * | |
267 | * We reduce the formula to: | |
268 | * | |
269 | * Hadj = H * (ASA + 128) / 256 | |
270 | * | |
271 | * H is in the range of -4096 to 4095. The magnetometer has a range of | |
272 | * +-1229uT. To go from the raw value to uT is: | |
273 | * | |
274 | * HuT = H * 1229/4096, or roughly, 3/10. | |
275 | * | |
eb03610a | 276 | * Since 1uT = 0.01 gauss, our final scale factor becomes: |
694e1b5f | 277 | * |
bef44abc BS |
278 | * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 |
279 | * Hadj = H * ((ASA + 128) * 0.003) / 256 | |
694e1b5f JC |
280 | * |
281 | * Since ASA doesn't change, we cache the resultant scale factor into the | |
282 | * device context in ak8975_setup(). | |
283 | */ | |
6027c077 SP |
284 | if (data->chipset == AK8963) { |
285 | /* | |
286 | * H range is +-8190 and magnetometer range is +-4912. | |
287 | * So HuT using the above explanation for 8975, | |
288 | * 4912/8190 = ~ 6/10. | |
289 | * So the Hadj should use 6/10 instead of 3/10. | |
290 | */ | |
291 | data->raw_to_gauss[0] = RAW_TO_GAUSS_8963(data->asa[0]); | |
292 | data->raw_to_gauss[1] = RAW_TO_GAUSS_8963(data->asa[1]); | |
293 | data->raw_to_gauss[2] = RAW_TO_GAUSS_8963(data->asa[2]); | |
294 | } else { | |
295 | data->raw_to_gauss[0] = RAW_TO_GAUSS_8975(data->asa[0]); | |
296 | data->raw_to_gauss[1] = RAW_TO_GAUSS_8975(data->asa[1]); | |
297 | data->raw_to_gauss[2] = RAW_TO_GAUSS_8975(data->asa[2]); | |
298 | } | |
3285aae1 AC |
299 | |
300 | return 0; | |
301 | } | |
302 | ||
01fbb478 AC |
303 | static int wait_conversion_complete_gpio(struct ak8975_data *data) |
304 | { | |
305 | struct i2c_client *client = data->client; | |
01fbb478 AC |
306 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; |
307 | int ret; | |
308 | ||
309 | /* Wait for the conversion to complete. */ | |
310 | while (timeout_ms) { | |
311 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); | |
312 | if (gpio_get_value(data->eoc_gpio)) | |
313 | break; | |
314 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; | |
315 | } | |
316 | if (!timeout_ms) { | |
317 | dev_err(&client->dev, "Conversion timeout happened\n"); | |
318 | return -EINVAL; | |
319 | } | |
320 | ||
c411f600 JC |
321 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
322 | if (ret < 0) | |
01fbb478 | 323 | dev_err(&client->dev, "Error in reading ST1\n"); |
c411f600 JC |
324 | |
325 | return ret; | |
01fbb478 AC |
326 | } |
327 | ||
328 | static int wait_conversion_complete_polled(struct ak8975_data *data) | |
329 | { | |
330 | struct i2c_client *client = data->client; | |
331 | u8 read_status; | |
332 | u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; | |
333 | int ret; | |
334 | ||
335 | /* Wait for the conversion to complete. */ | |
336 | while (timeout_ms) { | |
337 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); | |
c411f600 | 338 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); |
01fbb478 AC |
339 | if (ret < 0) { |
340 | dev_err(&client->dev, "Error in reading ST1\n"); | |
341 | return ret; | |
342 | } | |
c411f600 | 343 | read_status = ret; |
01fbb478 AC |
344 | if (read_status) |
345 | break; | |
346 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; | |
347 | } | |
348 | if (!timeout_ms) { | |
349 | dev_err(&client->dev, "Conversion timeout happened\n"); | |
350 | return -EINVAL; | |
351 | } | |
94a6d5cf | 352 | |
01fbb478 AC |
353 | return read_status; |
354 | } | |
355 | ||
94a6d5cf JA |
356 | /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ |
357 | static int wait_conversion_complete_interrupt(struct ak8975_data *data) | |
358 | { | |
359 | int ret; | |
360 | ||
361 | ret = wait_event_timeout(data->data_ready_queue, | |
362 | test_bit(0, &data->flags), | |
363 | AK8975_DATA_READY_TIMEOUT); | |
364 | clear_bit(0, &data->flags); | |
365 | ||
366 | return ret > 0 ? 0 : -ETIME; | |
367 | } | |
368 | ||
3285aae1 AC |
369 | /* |
370 | * Emits the raw flux value for the x, y, or z axis. | |
371 | */ | |
694e1b5f | 372 | static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) |
3285aae1 | 373 | { |
338473c8 | 374 | struct ak8975_data *data = iio_priv(indio_dev); |
3285aae1 | 375 | struct i2c_client *client = data->client; |
3285aae1 AC |
376 | u16 meas_reg; |
377 | s16 raw; | |
3285aae1 AC |
378 | int ret; |
379 | ||
380 | mutex_lock(&data->lock); | |
381 | ||
3285aae1 AC |
382 | /* Set up the device for taking a sample. */ |
383 | ret = ak8975_write_data(client, | |
384 | AK8975_REG_CNTL, | |
385 | AK8975_REG_CNTL_MODE_ONCE, | |
386 | AK8975_REG_CNTL_MODE_MASK, | |
387 | AK8975_REG_CNTL_MODE_SHIFT); | |
388 | if (ret < 0) { | |
389 | dev_err(&client->dev, "Error in setting operating mode\n"); | |
390 | goto exit; | |
391 | } | |
392 | ||
393 | /* Wait for the conversion to complete. */ | |
94a6d5cf JA |
394 | if (data->eoc_irq) |
395 | ret = wait_conversion_complete_interrupt(data); | |
396 | else if (gpio_is_valid(data->eoc_gpio)) | |
01fbb478 AC |
397 | ret = wait_conversion_complete_gpio(data); |
398 | else | |
399 | ret = wait_conversion_complete_polled(data); | |
400 | if (ret < 0) | |
3285aae1 | 401 | goto exit; |
3285aae1 | 402 | |
94a6d5cf | 403 | /* This will be executed only for non-interrupt based waiting case */ |
c411f600 JC |
404 | if (ret & AK8975_REG_ST1_DRDY_MASK) { |
405 | ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2); | |
3285aae1 AC |
406 | if (ret < 0) { |
407 | dev_err(&client->dev, "Error in reading ST2\n"); | |
408 | goto exit; | |
409 | } | |
c411f600 JC |
410 | if (ret & (AK8975_REG_ST2_DERR_MASK | |
411 | AK8975_REG_ST2_HOFL_MASK)) { | |
412 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); | |
3285aae1 AC |
413 | ret = -EINVAL; |
414 | goto exit; | |
415 | } | |
416 | } | |
417 | ||
418 | /* Read the flux value from the appropriate register | |
419 | (the register is specified in the iio device attributes). */ | |
c411f600 | 420 | ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]); |
3285aae1 AC |
421 | if (ret < 0) { |
422 | dev_err(&client->dev, "Read axis data fails\n"); | |
423 | goto exit; | |
424 | } | |
c411f600 | 425 | meas_reg = ret; |
3285aae1 AC |
426 | |
427 | mutex_unlock(&data->lock); | |
428 | ||
429 | /* Endian conversion of the measured values. */ | |
430 | raw = (s16) (le16_to_cpu(meas_reg)); | |
431 | ||
432 | /* Clamp to valid range. */ | |
433 | raw = clamp_t(s16, raw, -4096, 4095); | |
694e1b5f JC |
434 | *val = raw; |
435 | return IIO_VAL_INT; | |
3285aae1 AC |
436 | |
437 | exit: | |
438 | mutex_unlock(&data->lock); | |
439 | return ret; | |
440 | } | |
441 | ||
694e1b5f JC |
442 | static int ak8975_read_raw(struct iio_dev *indio_dev, |
443 | struct iio_chan_spec const *chan, | |
444 | int *val, int *val2, | |
445 | long mask) | |
446 | { | |
447 | struct ak8975_data *data = iio_priv(indio_dev); | |
448 | ||
449 | switch (mask) { | |
4d9948b3 | 450 | case IIO_CHAN_INFO_RAW: |
694e1b5f | 451 | return ak8975_read_axis(indio_dev, chan->address, val); |
c8a9f805 | 452 | case IIO_CHAN_INFO_SCALE: |
bef44abc BS |
453 | *val = 0; |
454 | *val2 = data->raw_to_gauss[chan->address]; | |
455 | return IIO_VAL_INT_PLUS_MICRO; | |
694e1b5f JC |
456 | } |
457 | return -EINVAL; | |
458 | } | |
459 | ||
460 | #define AK8975_CHANNEL(axis, index) \ | |
461 | { \ | |
462 | .type = IIO_MAGN, \ | |
463 | .modified = 1, \ | |
464 | .channel2 = IIO_MOD_##axis, \ | |
3a0b4422 JC |
465 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
466 | BIT(IIO_CHAN_INFO_SCALE), \ | |
694e1b5f JC |
467 | .address = index, \ |
468 | } | |
469 | ||
470 | static const struct iio_chan_spec ak8975_channels[] = { | |
471 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), | |
472 | }; | |
473 | ||
6fe8135f | 474 | static const struct iio_info ak8975_info = { |
694e1b5f | 475 | .read_raw = &ak8975_read_raw, |
6fe8135f JC |
476 | .driver_module = THIS_MODULE, |
477 | }; | |
478 | ||
d913971e SP |
479 | static const struct acpi_device_id ak_acpi_match[] = { |
480 | {"AK8975", AK8975}, | |
481 | {"AK8963", AK8963}, | |
482 | {"INVN6500", AK8963}, | |
483 | { }, | |
484 | }; | |
485 | MODULE_DEVICE_TABLE(acpi, ak_acpi_match); | |
486 | ||
487 | static char *ak8975_match_acpi_device(struct device *dev, | |
488 | enum asahi_compass_chipset *chipset) | |
489 | { | |
490 | const struct acpi_device_id *id; | |
491 | ||
492 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | |
493 | if (!id) | |
494 | return NULL; | |
495 | *chipset = (int)id->driver_data; | |
496 | ||
497 | return (char *)dev_name(dev); | |
498 | } | |
499 | ||
4ae1c61f | 500 | static int ak8975_probe(struct i2c_client *client, |
3285aae1 AC |
501 | const struct i2c_device_id *id) |
502 | { | |
503 | struct ak8975_data *data; | |
338473c8 JC |
504 | struct iio_dev *indio_dev; |
505 | int eoc_gpio; | |
3285aae1 | 506 | int err; |
d913971e | 507 | char *name = NULL; |
3285aae1 | 508 | |
3285aae1 | 509 | /* Grab and set up the supplied GPIO. */ |
f4b7f751 | 510 | if (client->dev.platform_data) |
f6d838d7 | 511 | eoc_gpio = *(int *)(client->dev.platform_data); |
f4b7f751 JA |
512 | else if (client->dev.of_node) |
513 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); | |
514 | else | |
515 | eoc_gpio = -1; | |
516 | ||
517 | if (eoc_gpio == -EPROBE_DEFER) | |
518 | return -EPROBE_DEFER; | |
3285aae1 | 519 | |
01fbb478 AC |
520 | /* We may not have a GPIO based IRQ to scan, that is fine, we will |
521 | poll if so */ | |
7c6c9368 | 522 | if (gpio_is_valid(eoc_gpio)) { |
82f2acdc | 523 | err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); |
01fbb478 AC |
524 | if (err < 0) { |
525 | dev_err(&client->dev, | |
526 | "failed to request GPIO %d, error %d\n", | |
338473c8 JC |
527 | eoc_gpio, err); |
528 | goto exit; | |
01fbb478 | 529 | } |
7c6c9368 | 530 | } |
3285aae1 | 531 | |
338473c8 | 532 | /* Register with IIO */ |
7cbb7537 | 533 | indio_dev = iio_device_alloc(sizeof(*data)); |
338473c8 JC |
534 | if (indio_dev == NULL) { |
535 | err = -ENOMEM; | |
536 | goto exit_gpio; | |
537 | } | |
538 | data = iio_priv(indio_dev); | |
40f32d93 | 539 | i2c_set_clientdata(client, indio_dev); |
94a6d5cf JA |
540 | |
541 | data->client = client; | |
542 | data->eoc_gpio = eoc_gpio; | |
543 | data->eoc_irq = 0; | |
544 | ||
d913971e SP |
545 | /* id will be NULL when enumerated via ACPI */ |
546 | if (id) { | |
547 | data->chipset = | |
548 | (enum asahi_compass_chipset)(id->driver_data); | |
549 | name = (char *) id->name; | |
550 | } else if (ACPI_HANDLE(&client->dev)) | |
551 | name = ak8975_match_acpi_device(&client->dev, &data->chipset); | |
552 | else { | |
553 | err = -ENOSYS; | |
554 | goto exit_free_iio; | |
555 | } | |
556 | dev_dbg(&client->dev, "Asahi compass chip %s\n", name); | |
6027c077 | 557 | |
3285aae1 AC |
558 | /* Perform some basic start-of-day setup of the device. */ |
559 | err = ak8975_setup(client); | |
560 | if (err < 0) { | |
561 | dev_err(&client->dev, "AK8975 initialization fails\n"); | |
ad31d250 | 562 | goto exit_free_iio; |
3285aae1 AC |
563 | } |
564 | ||
338473c8 JC |
565 | data->client = client; |
566 | mutex_init(&data->lock); | |
338473c8 JC |
567 | data->eoc_gpio = eoc_gpio; |
568 | indio_dev->dev.parent = &client->dev; | |
694e1b5f JC |
569 | indio_dev->channels = ak8975_channels; |
570 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); | |
338473c8 JC |
571 | indio_dev->info = &ak8975_info; |
572 | indio_dev->modes = INDIO_DIRECT_MODE; | |
d913971e | 573 | indio_dev->name = name; |
338473c8 | 574 | err = iio_device_register(indio_dev); |
3285aae1 AC |
575 | if (err < 0) |
576 | goto exit_free_iio; | |
577 | ||
578 | return 0; | |
579 | ||
580 | exit_free_iio: | |
7cbb7537 | 581 | iio_device_free(indio_dev); |
94a6d5cf JA |
582 | if (data->eoc_irq) |
583 | free_irq(data->eoc_irq, data); | |
3285aae1 | 584 | exit_gpio: |
7c6c9368 | 585 | if (gpio_is_valid(eoc_gpio)) |
338473c8 | 586 | gpio_free(eoc_gpio); |
3285aae1 AC |
587 | exit: |
588 | return err; | |
589 | } | |
590 | ||
447d4f29 | 591 | static int ak8975_remove(struct i2c_client *client) |
3285aae1 | 592 | { |
338473c8 JC |
593 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
594 | struct ak8975_data *data = iio_priv(indio_dev); | |
3285aae1 | 595 | |
338473c8 | 596 | iio_device_unregister(indio_dev); |
3285aae1 | 597 | |
94a6d5cf JA |
598 | if (data->eoc_irq) |
599 | free_irq(data->eoc_irq, data); | |
600 | ||
d2fffd6c JC |
601 | if (gpio_is_valid(data->eoc_gpio)) |
602 | gpio_free(data->eoc_gpio); | |
603 | ||
7cbb7537 | 604 | iio_device_free(indio_dev); |
3285aae1 AC |
605 | |
606 | return 0; | |
607 | } | |
608 | ||
609 | static const struct i2c_device_id ak8975_id[] = { | |
6027c077 SP |
610 | {"ak8975", AK8975}, |
611 | {"ak8963", AK8963}, | |
3285aae1 AC |
612 | {} |
613 | }; | |
614 | ||
615 | MODULE_DEVICE_TABLE(i2c, ak8975_id); | |
616 | ||
54461c30 OJ |
617 | static const struct of_device_id ak8975_of_match[] = { |
618 | { .compatible = "asahi-kasei,ak8975", }, | |
619 | { .compatible = "ak8975", }, | |
620 | { } | |
621 | }; | |
622 | MODULE_DEVICE_TABLE(of, ak8975_of_match); | |
623 | ||
3285aae1 AC |
624 | static struct i2c_driver ak8975_driver = { |
625 | .driver = { | |
626 | .name = "ak8975", | |
54461c30 | 627 | .of_match_table = ak8975_of_match, |
d913971e | 628 | .acpi_match_table = ACPI_PTR(ak_acpi_match), |
3285aae1 AC |
629 | }, |
630 | .probe = ak8975_probe, | |
e543acf0 | 631 | .remove = ak8975_remove, |
3285aae1 AC |
632 | .id_table = ak8975_id, |
633 | }; | |
6e5af184 | 634 | module_i2c_driver(ak8975_driver); |
3285aae1 AC |
635 | |
636 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); | |
637 | MODULE_DESCRIPTION("AK8975 magnetometer driver"); | |
638 | MODULE_LICENSE("GPL"); |