1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 <linux/iio/iio.h>
34#include <linux/iio/sysfs.h>
35
36
37
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
82
83#define AK8975_MAX_CONVERSION_TIMEOUT 500
84#define AK8975_CONVERSION_DONE_POLL_TIME 10
85
86
87
88
89struct ak8975_data {
90 struct i2c_client *client;
91 struct attribute_group attrs;
92 struct mutex lock;
93 u8 asa[3];
94 long raw_to_gauss[3];
95 u8 reg_cache[AK8975_MAX_REGS];
96 int eoc_gpio;
97 int eoc_irq;
98};
99
100static const int ak8975_index_to_reg[] = {
101 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
102};
103
104
105
106
107static int ak8975_write_data(struct i2c_client *client,
108 u8 reg, u8 val, u8 mask, u8 shift)
109{
110 struct iio_dev *indio_dev = i2c_get_clientdata(client);
111 struct ak8975_data *data = iio_priv(indio_dev);
112 u8 regval;
113 int ret;
114
115 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
116 ret = i2c_smbus_write_byte_data(client, reg, regval);
117 if (ret < 0) {
118 dev_err(&client->dev, "Write to device fails status %x\n", ret);
119 return ret;
120 }
121 data->reg_cache[reg] = regval;
122
123 return 0;
124}
125
126
127
128
129static int ak8975_read_data(struct i2c_client *client,
130 u8 reg, u8 length, u8 *buffer)
131{
132 int ret;
133 struct i2c_msg msg[2] = {
134 {
135 .addr = client->addr,
136 .flags = I2C_M_NOSTART,
137 .len = 1,
138 .buf = ®,
139 }, {
140 .addr = client->addr,
141 .flags = I2C_M_RD,
142 .len = length,
143 .buf = buffer,
144 }
145 };
146
147 ret = i2c_transfer(client->adapter, msg, 2);
148 if (ret < 0) {
149 dev_err(&client->dev, "Read from device fails\n");
150 return ret;
151 }
152
153 return 0;
154}
155
156
157
158
159
160static int ak8975_setup(struct i2c_client *client)
161{
162 struct iio_dev *indio_dev = i2c_get_clientdata(client);
163 struct ak8975_data *data = iio_priv(indio_dev);
164 u8 device_id;
165 int ret;
166
167
168 ret = ak8975_read_data(client, AK8975_REG_WIA, 1, &device_id);
169 if (ret < 0) {
170 dev_err(&client->dev, "Error reading WIA\n");
171 return ret;
172 }
173 if (device_id != AK8975_DEVICE_ID) {
174 dev_err(&client->dev, "Device ak8975 not found\n");
175 return -ENODEV;
176 }
177
178
179 ret = ak8975_write_data(client,
180 AK8975_REG_CNTL,
181 AK8975_REG_CNTL_MODE_FUSE_ROM,
182 AK8975_REG_CNTL_MODE_MASK,
183 AK8975_REG_CNTL_MODE_SHIFT);
184 if (ret < 0) {
185 dev_err(&client->dev, "Error in setting fuse access mode\n");
186 return ret;
187 }
188
189
190 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
191 if (ret < 0) {
192 dev_err(&client->dev, "Not able to read asa data\n");
193 return ret;
194 }
195
196
197 ret = ak8975_write_data(client,
198 AK8975_REG_CNTL,
199 AK8975_REG_CNTL_MODE_POWER_DOWN,
200 AK8975_REG_CNTL_MODE_MASK,
201 AK8975_REG_CNTL_MODE_SHIFT);
202 if (ret < 0) {
203 dev_err(&client->dev, "Error in setting power-down mode\n");
204 return ret;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
243 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
244 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
245
246 return 0;
247}
248
249static int wait_conversion_complete_gpio(struct ak8975_data *data)
250{
251 struct i2c_client *client = data->client;
252 u8 read_status;
253 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
254 int ret;
255
256
257 while (timeout_ms) {
258 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
259 if (gpio_get_value(data->eoc_gpio))
260 break;
261 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
262 }
263 if (!timeout_ms) {
264 dev_err(&client->dev, "Conversion timeout happened\n");
265 return -EINVAL;
266 }
267
268 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
269 if (ret < 0) {
270 dev_err(&client->dev, "Error in reading ST1\n");
271 return ret;
272 }
273 return read_status;
274}
275
276static int wait_conversion_complete_polled(struct ak8975_data *data)
277{
278 struct i2c_client *client = data->client;
279 u8 read_status;
280 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
281 int ret;
282
283
284 while (timeout_ms) {
285 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
286 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
287 if (ret < 0) {
288 dev_err(&client->dev, "Error in reading ST1\n");
289 return ret;
290 }
291 if (read_status)
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 return read_status;
300}
301
302
303
304
305static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
306{
307 struct ak8975_data *data = iio_priv(indio_dev);
308 struct i2c_client *client = data->client;
309 u16 meas_reg;
310 s16 raw;
311 u8 read_status;
312 int ret;
313
314 mutex_lock(&data->lock);
315
316
317 ret = ak8975_write_data(client,
318 AK8975_REG_CNTL,
319 AK8975_REG_CNTL_MODE_ONCE,
320 AK8975_REG_CNTL_MODE_MASK,
321 AK8975_REG_CNTL_MODE_SHIFT);
322 if (ret < 0) {
323 dev_err(&client->dev, "Error in setting operating mode\n");
324 goto exit;
325 }
326
327
328 if (gpio_is_valid(data->eoc_gpio))
329 ret = wait_conversion_complete_gpio(data);
330 else
331 ret = wait_conversion_complete_polled(data);
332 if (ret < 0)
333 goto exit;
334
335 read_status = ret;
336
337 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
338 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
339 if (ret < 0) {
340 dev_err(&client->dev, "Error in reading ST2\n");
341 goto exit;
342 }
343 if (read_status & (AK8975_REG_ST2_DERR_MASK |
344 AK8975_REG_ST2_HOFL_MASK)) {
345 dev_err(&client->dev, "ST2 status error 0x%x\n",
346 read_status);
347 ret = -EINVAL;
348 goto exit;
349 }
350 }
351
352
353
354 ret = ak8975_read_data(client, ak8975_index_to_reg[index],
355 2, (u8 *)&meas_reg);
356 if (ret < 0) {
357 dev_err(&client->dev, "Read axis data fails\n");
358 goto exit;
359 }
360
361 mutex_unlock(&data->lock);
362
363
364 raw = (s16) (le16_to_cpu(meas_reg));
365
366
367 raw = clamp_t(s16, raw, -4096, 4095);
368 *val = raw;
369 return IIO_VAL_INT;
370
371exit:
372 mutex_unlock(&data->lock);
373 return ret;
374}
375
376static int ak8975_read_raw(struct iio_dev *indio_dev,
377 struct iio_chan_spec const *chan,
378 int *val, int *val2,
379 long mask)
380{
381 struct ak8975_data *data = iio_priv(indio_dev);
382
383 switch (mask) {
384 case IIO_CHAN_INFO_RAW:
385 return ak8975_read_axis(indio_dev, chan->address, val);
386 case IIO_CHAN_INFO_SCALE:
387 *val = data->raw_to_gauss[chan->address];
388 return IIO_VAL_INT;
389 }
390 return -EINVAL;
391}
392
393#define AK8975_CHANNEL(axis, index) \
394 { \
395 .type = IIO_MAGN, \
396 .modified = 1, \
397 .channel2 = IIO_MOD_##axis, \
398 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
399 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
400 .address = index, \
401 }
402
403static const struct iio_chan_spec ak8975_channels[] = {
404 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
405};
406
407static const struct iio_info ak8975_info = {
408 .read_raw = &ak8975_read_raw,
409 .driver_module = THIS_MODULE,
410};
411
412static int __devinit ak8975_probe(struct i2c_client *client,
413 const struct i2c_device_id *id)
414{
415 struct ak8975_data *data;
416 struct iio_dev *indio_dev;
417 int eoc_gpio;
418 int err;
419
420
421 if (client->dev.platform_data == NULL)
422 eoc_gpio = -1;
423 else
424 eoc_gpio = *(int *)(client->dev.platform_data);
425
426
427
428 if (gpio_is_valid(eoc_gpio)) {
429 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
430 if (err < 0) {
431 dev_err(&client->dev,
432 "failed to request GPIO %d, error %d\n",
433 eoc_gpio, err);
434 goto exit;
435 }
436 }
437
438
439 indio_dev = iio_device_alloc(sizeof(*data));
440 if (indio_dev == NULL) {
441 err = -ENOMEM;
442 goto exit_gpio;
443 }
444 data = iio_priv(indio_dev);
445 i2c_set_clientdata(client, indio_dev);
446
447 err = ak8975_setup(client);
448 if (err < 0) {
449 dev_err(&client->dev, "AK8975 initialization fails\n");
450 goto exit_free_iio;
451 }
452
453 data->client = client;
454 mutex_init(&data->lock);
455 data->eoc_irq = client->irq;
456 data->eoc_gpio = eoc_gpio;
457 indio_dev->dev.parent = &client->dev;
458 indio_dev->channels = ak8975_channels;
459 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
460 indio_dev->info = &ak8975_info;
461 indio_dev->modes = INDIO_DIRECT_MODE;
462
463 err = iio_device_register(indio_dev);
464 if (err < 0)
465 goto exit_free_iio;
466
467 return 0;
468
469exit_free_iio:
470 iio_device_free(indio_dev);
471exit_gpio:
472 if (gpio_is_valid(eoc_gpio))
473 gpio_free(eoc_gpio);
474exit:
475 return err;
476}
477
478static int __devexit ak8975_remove(struct i2c_client *client)
479{
480 struct iio_dev *indio_dev = i2c_get_clientdata(client);
481 struct ak8975_data *data = iio_priv(indio_dev);
482
483 iio_device_unregister(indio_dev);
484
485 if (gpio_is_valid(data->eoc_gpio))
486 gpio_free(data->eoc_gpio);
487
488 iio_device_free(indio_dev);
489
490 return 0;
491}
492
493static const struct i2c_device_id ak8975_id[] = {
494 {"ak8975", 0},
495 {}
496};
497
498MODULE_DEVICE_TABLE(i2c, ak8975_id);
499
500static const struct of_device_id ak8975_of_match[] = {
501 { .compatible = "asahi-kasei,ak8975", },
502 { .compatible = "ak8975", },
503 { }
504};
505MODULE_DEVICE_TABLE(of, ak8975_of_match);
506
507static struct i2c_driver ak8975_driver = {
508 .driver = {
509 .name = "ak8975",
510 .of_match_table = ak8975_of_match,
511 },
512 .probe = ak8975_probe,
513 .remove = __devexit_p(ak8975_remove),
514 .id_table = ak8975_id,
515};
516module_i2c_driver(ak8975_driver);
517
518MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
519MODULE_DESCRIPTION("AK8975 magnetometer driver");
520MODULE_LICENSE("GPL");
521