2 * ak8975.c - Asahi Kasei Microdevices Corporation
3 * AK8975 three-axis electronic compass
5 * Copyright (C) 2010 Samsung Electronics
6 * Donggeun Kim <dg77.kim@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
17 #include <linux/workqueue.h>
18 #include <linux/mutex.h>
19 #include <linux/err.h>
20 #include <linux/i2c.h>
21 #include <linux/delay.h>
22 #include <linux/gpio.h>
23 #include <linux/completion.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/ak8975.h>
32 #define AK8975_WHO_AM_I 0x00
33 #define AK8975_INFO 0x01
34 #define AK8975_ST1 0x02
35 #define AK8975_HXL 0x03
36 #define AK8975_HXH 0x04
37 #define AK8975_HYL 0x05
38 #define AK8975_HYH 0x06
39 #define AK8975_HZL 0x07
40 #define AK8975_HZH 0x08
41 #define AK8975_ST2 0x09
42 #define AK8975_CNTL 0x0a
43 #define AK8975_ASTC 0x0c
44 #define AK8975_I2CDIS 0x0f
45 #define AK8975_ASAX 0x10
46 #define AK8975_ASAY 0x11
47 #define AK8975_ASAZ 0x12
49 #define AK8975_POWERDOWN_MODE (0x00)
50 #define AK8975_MEASUREMENT_MODE (0x01)
51 #define AK8975_SELFTEST_MODE (0x08)
52 #define AK8975_FUSE_ROM_MODE (0x0f)
54 #define AK8975_DRDY_MASK (1)
55 #define AK8975_DERR_SHIFT (2)
56 #define AK8975_DERR_MASK (1 << AK8975_DERR_SHIFT)
57 #define AK8975_HOFL_SHIFT (3)
58 #define AK8975_HOFL_MASK (1 << AK8975_HOFL_SHIFT)
59 #define AK8975_MODE_MASK (0xf)
60 #define AK8975_SELF_MASK (1 << 6)
61 #define AK8975_I2CDIS_MASK (1)
63 #define AK8975_OUT_MAX (4095)
64 #define AK8975_OUT_MIN (-4096)
66 #define AK8975_MAX_MEASUREMENT_TIME ((HZ * 60) / 1000)
68 #define AK8975_POWERDOWN_WAIT_TIME 100
70 #define AK8975_NUM_OF_XYZ_SENSOR_DATA 6
71 #define AK8975_NUM_OF_SENSOR_DATA 2
72 #define AK8975_NUM_OF_ASA_DATA 3
73 #define AK8975_NUM_OF_CNTL_DATA 2
74 #define AK8975_NUM_OF_BYTE_DATA 1
76 #define AK8975_DEV_ID 0x48
78 #define AK8975_X_MEASUREMENT 1
79 #define AK8975_Y_MEASUREMENT 2
80 #define AK8975_Z_MEASUREMENT 3
81 #define AK8975_XYZ_MEASUREMENT 4
90 struct i2c_client *client;
91 struct iio_dev *indio_dev;
92 struct work_struct work;
95 struct completion comp;
97 struct ak8975_data data;
100 u8 sa[AK8975_NUM_OF_ASA_DATA];
105 static int ak8975_write_reg(struct i2c_client *client, u8 *buffer, int length)
107 struct i2c_msg msg[] = {
109 .addr = client->addr,
115 return i2c_transfer(client->adapter, msg, 1);
118 static int ak8975_read_reg(struct i2c_client *client, u8 *buffer, int length)
120 struct i2c_msg msg[] = {
122 .addr = client->addr,
127 .addr = client->addr,
133 return i2c_transfer(client->adapter, msg, 2);
136 static void ak8975_update_data(struct ak8975_chip *ac)
138 u8 buffer[AK8975_NUM_OF_XYZ_SENSOR_DATA];
140 switch(ac->measurement_type) {
141 case AK8975_X_MEASUREMENT:
142 buffer[0] = AK8975_HXL;
143 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_SENSOR_DATA);
144 ac->data.x = (buffer[1] << BITS_PER_BYTE) | buffer[0];
146 case AK8975_Y_MEASUREMENT:
147 buffer[0] = AK8975_HYL;
148 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_SENSOR_DATA);
149 ac->data.y = (buffer[1] << BITS_PER_BYTE) | buffer[0];
151 case AK8975_Z_MEASUREMENT:
152 buffer[0] = AK8975_HZL;
153 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_SENSOR_DATA);
154 ac->data.z = (buffer[1] << BITS_PER_BYTE) | buffer[0];
156 case AK8975_XYZ_MEASUREMENT:
157 buffer[0] = AK8975_HXL;
158 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_XYZ_SENSOR_DATA);
159 ac->data.x = (buffer[1] << BITS_PER_BYTE) | buffer[0];
160 ac->data.y = (buffer[3] << BITS_PER_BYTE) | buffer[2];
161 ac->data.z = (buffer[5] << BITS_PER_BYTE) | buffer[4];
168 static void ak8975_work(struct work_struct *work)
170 struct ak8975_chip *ac = container_of(work, struct ak8975_chip, work);
171 static int counter = -1;
173 if (++counter == 0) {
174 enable_irq(ac->client->irq);
178 ak8975_update_data(ac);
181 enable_irq(ac->client->irq);
184 static irqreturn_t ak8975_irq(int irq, void *data)
186 struct ak8975_chip *ac = data;
188 if (!work_pending(&ac->work)) {
189 disable_irq_nosync(irq);
190 schedule_work(&ac->work);
192 dev_err(&ac->client->dev, "work_pending\n");
197 #define AK8975_INPUT(name, reg) \
198 static ssize_t ak8975_store_##name(struct device *dev, \
199 struct device_attribute *attr, const char *buf, size_t count) \
201 struct iio_dev *indio_dev = dev_get_drvdata(dev); \
202 struct ak8975_chip *ac = indio_dev->dev_data; \
205 u8 buffer[AK8975_NUM_OF_CNTL_DATA]; \
210 ret = strict_strtoul(buf, 10, &val); \
217 ret = ak8975_write_reg(ac->client, buffer, \
218 AK8975_NUM_OF_CNTL_DATA); \
225 static ssize_t ak8975_show_##name(struct device *dev, \
226 struct device_attribute *attr, char *buf) \
228 struct iio_dev *indio_dev = dev_get_drvdata(dev); \
229 struct ak8975_chip *ac = indio_dev->dev_data; \
233 ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_BYTE_DATA); \
234 return sprintf(buf, "%d\n", buffer); \
236 static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, \
237 ak8975_show_##name, ak8975_store_##name);
239 AK8975_INPUT(self_test_control, AK8975_ASTC);
240 AK8975_INPUT(control, AK8975_CNTL);
242 #define AK8975_OUTPUT_RAW(name, measurement_mode, type, field) \
243 static ssize_t ak8975_show_##name(struct device *dev, \
244 struct device_attribute *attr, char *buf) \
246 struct iio_dev *indio_dev = dev_get_drvdata(dev); \
247 struct ak8975_chip *ac = indio_dev->dev_data; \
250 mutex_lock(&ac->lock); \
252 buffer[0] = AK8975_CNTL; \
253 buffer[1] = measurement_mode; \
255 ac->measurement_type = type; \
257 ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_CNTL_DATA); \
258 wait_for_completion_timeout(&ac->comp, \
259 AK8975_MAX_MEASUREMENT_TIME); \
261 mutex_unlock(&ac->lock); \
263 if (ac->measurement_type == AK8975_XYZ_MEASUREMENT) \
264 return sprintf(buf, "%d %d %d\n", ac->data.x, \
265 ac->data.y, ac->data.z); \
267 return sprintf(buf, "%d\n", ac->data.field); \
269 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_##name, NULL);
271 AK8975_OUTPUT_RAW(magn_x_raw, AK8975_MEASUREMENT_MODE,
272 AK8975_X_MEASUREMENT, x);
273 AK8975_OUTPUT_RAW(magn_y_raw, AK8975_MEASUREMENT_MODE,
274 AK8975_Y_MEASUREMENT, y);
275 AK8975_OUTPUT_RAW(magn_z_raw, AK8975_MEASUREMENT_MODE,
276 AK8975_Z_MEASUREMENT, z);
277 AK8975_OUTPUT_RAW(magn_xyz_raw, AK8975_MEASUREMENT_MODE,
278 AK8975_XYZ_MEASUREMENT, x);
279 AK8975_OUTPUT_RAW(self_test_xyz_raw, AK8975_SELFTEST_MODE,
280 AK8975_XYZ_MEASUREMENT, x);
282 #define AK8975_OUTPUT_REG(name, reg) \
283 static ssize_t ak8975_show_##name(struct device *dev, \
284 struct device_attribute *attr, char *buf) \
286 struct iio_dev *indio_dev = dev_get_drvdata(dev); \
287 struct ak8975_chip *ac = indio_dev->dev_data; \
291 ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_BYTE_DATA); \
292 return sprintf(buf, "%d\n", buffer); \
294 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_##name, NULL);
296 AK8975_OUTPUT_REG(i2c_disable, AK8975_I2CDIS);
297 AK8975_OUTPUT_REG(status1, AK8975_ST1);
298 AK8975_OUTPUT_REG(status2, AK8975_ST2);
299 AK8975_OUTPUT_REG(hxl, AK8975_HXL);
300 AK8975_OUTPUT_REG(hxh, AK8975_HXH);
301 AK8975_OUTPUT_REG(hyl, AK8975_HYL);
302 AK8975_OUTPUT_REG(hyh, AK8975_HYH);
303 AK8975_OUTPUT_REG(hzl, AK8975_HZL);
304 AK8975_OUTPUT_REG(hzh, AK8975_HZH);
306 static ssize_t ak8975_show_calibbias(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct iio_dev *indio_dev = dev_get_drvdata(dev);
310 struct ak8975_chip *ac = indio_dev->dev_data;
311 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
313 return sprintf(buf, "%d\n", ac->sa[iio_attr->address]);
316 static IIO_DEVICE_ATTR(magn_x_calibbias, S_IRUGO,
317 ak8975_show_calibbias, NULL, 0);
318 static IIO_DEVICE_ATTR(magn_y_calibbias, S_IRUGO,
319 ak8975_show_calibbias, NULL, 1);
320 static IIO_DEVICE_ATTR(magn_z_calibbias, S_IRUGO,
321 ak8975_show_calibbias, NULL, 2);
323 static ssize_t ak8975_show_name(struct device *dev,
324 struct device_attribute *attr,
327 struct iio_dev *indio_dev = dev_get_drvdata(dev);
328 struct ak8975_chip *chip = indio_dev->dev_data;
329 return sprintf(buf, "%s\n", chip->client->name);
331 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_name, NULL);
333 static ssize_t ak8975_show_dev_id(struct device *dev,
334 struct device_attribute *attr,
337 struct iio_dev *indio_dev = dev_get_drvdata(dev);
338 struct ak8975_chip *chip = indio_dev->dev_data;
339 return sprintf(buf, "%d\n", chip->dev_id);
341 static DEVICE_ATTR(dev_id, S_IRUGO, ak8975_show_dev_id, NULL);
343 static ssize_t ak8975_show_dev_info(struct device *dev,
344 struct device_attribute *attr,
347 struct iio_dev *indio_dev = dev_get_drvdata(dev);
348 struct ak8975_chip *chip = indio_dev->dev_data;
349 return sprintf(buf, "%d\n", chip->dev_info);
351 static DEVICE_ATTR(dev_info, S_IRUGO, ak8975_show_dev_info, NULL);
353 static struct attribute *ak8975_attributes[] = {
355 &dev_attr_dev_id.attr,
356 &dev_attr_dev_info.attr,
357 &dev_attr_status1.attr,
358 &dev_attr_status2.attr,
359 &dev_attr_self_test_control.attr,
360 &dev_attr_control.attr,
361 &dev_attr_magn_x_raw.attr,
362 &dev_attr_magn_y_raw.attr,
363 &dev_attr_magn_z_raw.attr,
364 &dev_attr_magn_xyz_raw.attr,
365 &dev_attr_self_test_xyz_raw.attr,
366 &dev_attr_i2c_disable.attr,
373 &iio_dev_attr_magn_x_calibbias.dev_attr.attr,
374 &iio_dev_attr_magn_y_calibbias.dev_attr.attr,
375 &iio_dev_attr_magn_z_calibbias.dev_attr.attr,
379 static const struct attribute_group ak8975_group = {
380 .attrs = ak8975_attributes,
383 static void ak8975_initialize(struct ak8975_chip *ac)
385 u8 buffer_cntl[AK8975_NUM_OF_CNTL_DATA];
386 u8 buffer[AK8975_NUM_OF_ASA_DATA];
388 buffer_cntl[0] = AK8975_CNTL;
389 buffer_cntl[1] = AK8975_FUSE_ROM_MODE;
390 ak8975_write_reg(ac->client, buffer_cntl, AK8975_NUM_OF_CNTL_DATA);
391 udelay(AK8975_POWERDOWN_WAIT_TIME);
393 buffer[0] = AK8975_ASAX;
394 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_ASA_DATA);
396 /* Put the device in Power-down mode */
397 buffer_cntl[0] = AK8975_CNTL;
398 buffer_cntl[1] = AK8975_POWERDOWN_MODE;
399 ak8975_write_reg(ac->client, buffer_cntl, AK8975_NUM_OF_CNTL_DATA);
401 ac->sa[0] = buffer[0];
402 ac->sa[1] = buffer[1];
403 ac->sa[2] = buffer[2];
405 /* Get the device information */
406 buffer[0] = AK8975_INFO;
407 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_BYTE_DATA);
408 ac->dev_info = buffer[0];
411 static int ak8975_probe(struct i2c_client *client,
412 const struct i2c_device_id *id)
414 struct ak8975_chip *ac;
415 struct ak8975_platform_data *pdata;
419 ac = kzalloc(sizeof(struct ak8975_chip), GFP_KERNEL);
423 dev_id = AK8975_WHO_AM_I;
424 ak8975_read_reg(client, &dev_id, AK8975_NUM_OF_BYTE_DATA);
425 if (dev_id != AK8975_DEV_ID) {
426 dev_err(&client->dev, "failed to detect device id\n");
427 goto error_devid_detect;
431 pdata = client->dev.platform_data;
434 i2c_set_clientdata(client, ac);
436 INIT_WORK(&ac->work, ak8975_work);
437 mutex_init(&ac->lock);
438 init_completion(&ac->comp);
440 ac->indio_dev = iio_allocate_device();
441 if (!ac->indio_dev) {
443 goto error_allocate_iio_device;
446 ac->indio_dev->attrs = &ak8975_group;
447 ac->indio_dev->dev.parent = &client->dev;
448 ac->indio_dev->dev_data = ac;
449 ac->indio_dev->driver_module = THIS_MODULE;
450 ac->indio_dev->modes = INDIO_DIRECT_MODE;
452 ret = iio_device_register(ac->indio_dev);
454 goto error_register_iio;
456 if (client->irq > 0) {
457 ret = request_irq(client->irq, ak8975_irq, IRQF_TRIGGER_RISING,
458 "ak8975 compass", ac);
460 dev_err(&client->dev, "can't get IRQ %d, ret %d\n",
466 ak8975_initialize(ac);
468 pm_runtime_set_active(&client->dev);
470 dev_info(&client->dev, "%s registered\n", id->name);
474 iio_device_unregister(ac->indio_dev);
476 iio_free_device(ac->indio_dev);
477 error_allocate_iio_device:
483 static int __devexit ak8975_remove(struct i2c_client *client)
485 struct ak8975_chip *ac = i2c_get_clientdata(client);
487 wait_for_completion(&ac->comp);
488 sysfs_remove_group(&client->dev.kobj, &ak8975_group);
489 free_irq(client->irq, ac);
496 static int ak8975_suspend(struct device *dev)
498 struct i2c_client *client = to_i2c_client(dev);
499 u8 buffer[AK8975_NUM_OF_CNTL_DATA];
501 disable_irq_nosync(client->irq);
503 /* Put the device in Power-down mode */
504 buffer[0] = AK8975_CNTL;
505 buffer[1] = AK8975_POWERDOWN_MODE;
506 ak8975_write_reg(client, buffer, AK8975_NUM_OF_CNTL_DATA);
511 static int ak8975_resume(struct device *dev)
513 struct i2c_client *client = to_i2c_client(dev);
514 struct ak8975_chip *ac = i2c_get_clientdata(client);
516 ak8975_initialize(ac);
518 enable_irq(client->irq);
523 static struct dev_pm_ops ak8975_dev_pm_ops = {
524 .suspend = ak8975_suspend,
525 .resume = ak8975_resume,
528 #define AK8975_DEV_PM_OPS (&ak8975_dev_pm_ops)
530 #define AK8975_DEV_PM_OPS NULL
533 static const struct i2c_device_id ak8975_id[] = {
538 MODULE_DEVICE_TABLE(i2c, ak8975_id);
540 static struct i2c_driver ak8975_i2c_driver = {
543 .pm = AK8975_DEV_PM_OPS,
545 .probe = ak8975_probe,
546 .remove = __devexit_p(ak8975_remove),
547 .id_table = ak8975_id,
550 static int __init ak8975_init(void)
552 return i2c_add_driver(&ak8975_i2c_driver);
555 module_init(ak8975_init);
557 static void __exit ak8975_exit(void)
559 i2c_del_driver(&ak8975_i2c_driver);
562 module_exit(ak8975_exit);
564 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
565 MODULE_DESCRIPTION("AK8975 compass driver");
566 MODULE_LICENSE("GPL");