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/input-polldev.h>
24 #include <linux/completion.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/ak8975.h>
29 #define AK8975_WHO_AM_I 0x00
30 #define AK8975_INFO 0x01
31 #define AK8975_ST1 0x02
32 #define AK8975_HXL 0x03
33 #define AK8975_HXH 0x04
34 #define AK8975_HYL 0x05
35 #define AK8975_HYH 0x06
36 #define AK8975_HZL 0x07
37 #define AK8975_HZH 0x08
38 #define AK8975_ST2 0x09
39 #define AK8975_CNTL 0x0a
40 #define AK8975_ASTC 0x0c
41 #define AK8975_I2CDIS 0x0f
42 #define AK8975_ASAX 0x10
43 #define AK8975_ASAY 0x11
44 #define AK8975_ASAZ 0x12
46 #define AK8975_POWERDOWN_MODE (0x00)
47 #define AK8975_MEASUREMENT_MODE (0x01)
48 #define AK8975_SELFTEST_MODE (0x08)
49 #define AK8975_FUSE_ROM_MODE (0x0f)
51 #define AK8975_DRDY_MASK (1)
52 #define AK8975_DERR_SHIFT (2)
53 #define AK8975_DERR_MASK (1 << AK8975_DERR_SHIFT)
54 #define AK8975_HOFL_SHIFT (3)
55 #define AK8975_HOFL_MASK (1 << AK8975_HOFL_SHIFT)
56 #define AK8975_MODE_MASK (0xf)
57 #define AK8975_SELF_MASK (1 << 6)
58 #define AK8975_I2CDIS_MASK (1)
60 #define AK8975_OUT_MAX (4095)
61 #define AK8975_OUT_MIN (-4096)
63 #define AK8975_MAX_MEASUREMENT_TIME ((HZ * 60) / 1000)
65 #define AK8975_POWERDOWN_WAIT_TIME 100
67 #define AK8975_NUM_OF_SENSOR_DATA 6
68 #define AK8975_NUM_OF_ASA_DATA 3
69 #define AK8975_NUM_OF_CNTL_DATA 2
70 #define AK8975_NUM_OF_I2CDIS_DATA 2
71 #define AK8975_NUM_OF_ST2_DATA 1
73 #define AK8975_DEV_ID 0x48
82 struct i2c_client *client;
84 struct work_struct work;
86 struct input_polled_dev *ipdev;
88 struct completion comp;
92 struct ak8975_data data;
95 static int ak8975_write_reg(struct i2c_client *client, u8 *buffer, int length)
97 struct i2c_msg msg[] = {
105 return i2c_transfer(client->adapter, msg, 1);
108 static int ak8975_read_reg(struct i2c_client *client, u8 *buffer, int length)
110 struct i2c_msg msg[] = {
112 .addr = client->addr,
117 .addr = client->addr,
123 return i2c_transfer(client->adapter, msg, 2);
126 static void ak8975_update_data(struct ak8975_chip *ac)
128 u8 buffer[AK8975_NUM_OF_SENSOR_DATA];
130 mutex_lock(&ac->lock);
131 buffer[0] = AK8975_HXL;
132 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_SENSOR_DATA);
133 ac->data.x = (buffer[1] << BITS_PER_BYTE) | buffer[0];
134 ac->data.y = (buffer[3] << BITS_PER_BYTE) | buffer[2];
135 ac->data.z = (buffer[5] << BITS_PER_BYTE) | buffer[4];
136 mutex_unlock(&ac->lock);
139 static void ak8975_work(struct work_struct *work)
141 struct ak8975_chip *ac = container_of(work, struct ak8975_chip, work);
142 static int counter = -1;
144 if (++counter == 0) {
145 enable_irq(ac->client->irq);
149 ak8975_update_data(ac);
153 mutex_lock(&ac->lock);
154 input_report_abs(ac->ipdev->input, ABS_X, ac->data.x);
155 input_report_abs(ac->ipdev->input, ABS_Y, ac->data.y);
156 input_report_abs(ac->ipdev->input, ABS_Z, ac->data.z);
157 input_sync(ac->ipdev->input);
158 mutex_unlock(&ac->lock);
160 enable_irq(ac->client->irq);
163 static irqreturn_t ak8975_irq(int irq, void *data)
165 struct ak8975_chip *ac = data;
167 if (!work_pending(&ac->work)) {
168 disable_irq_nosync(irq);
169 schedule_work(&ac->work);
171 dev_err(&ac->client->dev, "work_pending\n");
176 static ssize_t ak8975_show_output(struct device *dev,
177 struct device_attribute *attr, char *buf)
179 struct ak8975_chip *ac = dev_get_drvdata(dev);
182 buffer[0] = AK8975_CNTL;
183 buffer[1] = AK8975_MEASUREMENT_MODE;
184 ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_CNTL_DATA);
185 wait_for_completion_timeout(&ac->comp, AK8975_MAX_MEASUREMENT_TIME);
187 return sprintf(buf, "%d %d %d\n",
188 ac->data.x, ac->data.y, ac->data.z);
190 static DEVICE_ATTR(output, S_IRUGO, ak8975_show_output, NULL);
192 static ssize_t ak8975_set_i2c_disable(struct device *dev,
193 struct device_attribute *attr, const char *buf,
196 struct ak8975_chip *ac = dev_get_drvdata(dev);
198 u8 buffer[AK8975_NUM_OF_I2CDIS_DATA];
201 mutex_lock(&ac->lock);
202 buffer[0] = AK8975_I2CDIS;
203 ret = strict_strtoul(buf, 10, &val);
205 dev_err(dev, "fail: conversion %s to number\n", buf);
206 mutex_unlock(&ac->lock);
209 buffer[1] = (u8) val & AK8975_I2CDIS_MASK;
210 ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_I2CDIS_DATA);
211 mutex_unlock(&ac->lock);
214 static DEVICE_ATTR(i2c_disable, S_IWUSR, NULL, ak8975_set_i2c_disable);
216 static ssize_t ak8975_show_overflow(struct device *dev,
217 struct device_attribute *attr, char *buf)
219 struct ak8975_chip *ac = dev_get_drvdata(dev);
223 ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_ST2_DATA);
224 buffer &= AK8975_HOFL_MASK;
225 buffer >>= AK8975_HOFL_SHIFT;
226 return sprintf(buf, "%d\n", buffer);
228 static DEVICE_ATTR(overflow, S_IRUGO, ak8975_show_overflow, NULL);
230 static ssize_t ak8975_show_data_error(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct ak8975_chip *ac = dev_get_drvdata(dev);
237 ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_ST2_DATA);
238 buffer &= AK8975_DERR_MASK;
239 buffer >>= AK8975_DERR_SHIFT;
240 return sprintf(buf, "%d\n", buffer);
242 static DEVICE_ATTR(data_error, S_IRUGO, ak8975_show_data_error, NULL);
244 static ssize_t ak8975_show_SA(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct ak8975_chip *ac = dev_get_drvdata(dev);
248 u8 buffer_cntl[AK8975_NUM_OF_CNTL_DATA];
249 u8 buffer[AK8975_NUM_OF_ASA_DATA];
251 buffer_cntl[0] = AK8975_CNTL;
252 buffer_cntl[1] = AK8975_FUSE_ROM_MODE;
253 ak8975_write_reg(ac->client, buffer_cntl, AK8975_NUM_OF_CNTL_DATA);
254 udelay(AK8975_POWERDOWN_WAIT_TIME);
256 buffer[0] = AK8975_ASAX;
257 ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_ASA_DATA);
259 buffer_cntl[0] = AK8975_CNTL;
260 buffer_cntl[1] = AK8975_POWERDOWN_MODE;
261 ak8975_write_reg(ac->client, buffer_cntl, AK8975_NUM_OF_CNTL_DATA);
262 udelay(AK8975_POWERDOWN_WAIT_TIME);
263 return sprintf(buf, "%d %d %d\n", buffer[0], buffer[1], buffer[2]);
265 static DEVICE_ATTR(SA, S_IRUGO, ak8975_show_SA, NULL);
267 static struct attribute *ak8975_attributes[] = {
268 &dev_attr_output.attr,
269 &dev_attr_i2c_disable.attr,
270 &dev_attr_overflow.attr,
271 &dev_attr_data_error.attr,
276 static const struct attribute_group ak8975_group = {
277 .attrs = ak8975_attributes,
280 static void ak8975_initialize(struct ak8975_chip *ac)
282 u8 buffer[AK8975_NUM_OF_CNTL_DATA];
284 /* Put the device in Power-down mode */
285 buffer[0] = AK8975_CNTL;
286 buffer[1] = AK8975_POWERDOWN_MODE;
287 ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_CNTL_DATA);
289 udelay(AK8975_POWERDOWN_WAIT_TIME);
292 #ifdef CONFIG_INPUT_POLLDEV
293 static void ak8975_ipdev_poll(struct input_polled_dev *dev)
295 struct ak8975_chip *ac = dev->private;
296 u8 buffer[AK8975_NUM_OF_CNTL_DATA];
298 mutex_lock(&ac->lock);
300 buffer[0] = AK8975_CNTL;
301 buffer[1] = AK8975_MEASUREMENT_MODE;
302 ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_CNTL_DATA);
303 wait_for_completion_timeout(&ac->comp, AK8975_MAX_MEASUREMENT_TIME);
305 mutex_unlock(&ac->lock);
308 static int ak8975_register_input_dev(struct ak8975_chip *ac)
310 struct i2c_client *client = ac->client;
311 struct input_polled_dev *ipdev;
314 ac->ipdev = ipdev = input_allocate_polled_device();
316 dev_err(&client->dev, "fail: allocate poll input device\n");
322 ipdev->poll = ak8975_ipdev_poll;
323 if (ac->poll_interval)
324 ipdev->poll_interval = ac->poll_interval;
325 ipdev->input->name = "ak8975 compass";
326 ipdev->input->id.bustype = BUS_I2C;
327 ipdev->input->dev.parent = &client->dev;
328 ipdev->input->evbit[0] = BIT_MASK(EV_ABS);
330 input_set_abs_params(ipdev->input, ABS_X, AK8975_OUT_MIN,
331 AK8975_OUT_MAX, 0, 0);
332 input_set_abs_params(ipdev->input, ABS_Y, AK8975_OUT_MIN,
333 AK8975_OUT_MAX, 0, 0);
334 input_set_abs_params(ipdev->input, ABS_Z, AK8975_OUT_MIN,
335 AK8975_OUT_MAX, 0, 0);
337 ret = input_register_polled_device(ipdev);
339 dev_err(&client->dev, "fail: register to poll input device\n");
340 goto error_reg_input;
346 input_free_polled_device(ipdev);
351 static void ak8975_unregister_input_dev(struct ak8975_chip *ac)
353 input_unregister_polled_device(ac->ipdev);
354 input_free_polled_device(ac->ipdev);
357 static int ak8975_register_input_dev(struct ak8975_chip *ac)
362 static void ak8975_unregister_input_dev(struct ak8975_chip *ac)
367 static int ak8975_probe(struct i2c_client *client,
368 const struct i2c_device_id *id)
370 struct ak8975_chip *ac;
371 struct ak8975_platform_data *pdata;
375 ac = kzalloc(sizeof(struct ak8975_chip), GFP_KERNEL);
379 dev_id = AK8975_WHO_AM_I;
380 ak8975_read_reg(client, &dev_id, AK8975_NUM_OF_ST2_DATA);
381 if (dev_id != AK8975_DEV_ID) {
382 dev_err(&client->dev, "failed to detect device id\n");
383 goto error_devid_detect;
386 pdata = client->dev.platform_data;
388 ac->poll_interval = pdata->poll_interval;
390 i2c_set_clientdata(client, ac);
392 INIT_WORK(&ac->work, ak8975_work);
393 mutex_init(&ac->lock);
394 init_completion(&ac->comp);
396 if (client->irq > 0) {
397 ret = request_irq(client->irq, ak8975_irq, IRQF_TRIGGER_RISING,
398 "ak8975 compass", ac);
400 dev_err(&client->dev, "can't get IRQ %d, ret %d\n",
406 ret = sysfs_create_group(&client->dev.kobj, &ak8975_group);
408 dev_err(&client->dev, "Creating ak8975 attribute group failed");
412 ret = ak8975_register_input_dev(ac);
414 dev_err(&client->dev, "Registering ak8975 input device failed");
418 ak8975_initialize(ac);
420 pm_runtime_set_active(&client->dev);
422 dev_info(&client->dev, "%s registered\n", id->name);
426 sysfs_remove_group(&client->dev.kobj, &ak8975_group);
428 free_irq(client->irq, ac);
435 static int __devexit ak8975_remove(struct i2c_client *client)
437 struct ak8975_chip *ac = i2c_get_clientdata(client);
439 wait_for_completion(&ac->comp);
440 ak8975_unregister_input_dev(ac);
441 sysfs_remove_group(&client->dev.kobj, &ak8975_group);
442 free_irq(client->irq, ac);
448 static int ak8975_suspend(struct device *dev)
450 struct i2c_client *client = to_i2c_client(dev);
451 u8 buffer[AK8975_NUM_OF_CNTL_DATA];
453 disable_irq_nosync(client->irq);
455 /* Put the device in Power-down mode */
456 buffer[0] = AK8975_CNTL;
457 buffer[1] = AK8975_POWERDOWN_MODE;
458 ak8975_write_reg(client, buffer, AK8975_NUM_OF_CNTL_DATA);
463 static int ak8975_resume(struct device *dev)
465 struct i2c_client *client = to_i2c_client(dev);
466 struct ak8975_chip *ac = i2c_get_clientdata(client);
468 ak8975_initialize(ac);
470 enable_irq(client->irq);
475 static struct dev_pm_ops ak8975_dev_pm_ops = {
476 .suspend = ak8975_suspend,
477 .resume = ak8975_resume,
480 #define AK8975_DEV_PM_OPS (&ak8975_dev_pm_ops)
482 #define AK8975_DEV_PM_OPS NULL
485 static const struct i2c_device_id ak8975_id[] = {
490 MODULE_DEVICE_TABLE(i2c, ak8975_id);
492 static struct i2c_driver ak8975_i2c_driver = {
495 .pm = AK8975_DEV_PM_OPS,
497 .probe = ak8975_probe,
498 .remove = __devexit_p(ak8975_remove),
499 .id_table = ak8975_id,
502 static int __init ak8975_init(void)
504 return i2c_add_driver(&ak8975_i2c_driver);
507 module_init(ak8975_init);
509 static void __exit ak8975_exit(void)
511 i2c_del_driver(&ak8975_i2c_driver);
514 module_exit(ak8975_exit);
516 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
517 MODULE_DESCRIPTION("AK8975 compass driver");
518 MODULE_LICENSE("GPL");