upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / misc / ak8975.c
1 /*
2  *  ak8975.c - Asahi Kasei Microdevices Corporation
3  *             AK8975 three-axis electronic compass
4  *
5  *  Copyright (C) 2010 Samsung Electronics
6  *  Donggeun Kim <dg77.kim@samsung.com>
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 version 2 as
10  * published by the Free Software Foundation.
11  */
12
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>
28
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
45
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)
50
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)
59
60 #define AK8975_OUT_MAX          (4095)
61 #define AK8975_OUT_MIN          (-4096)
62
63 #define AK8975_MAX_MEASUREMENT_TIME     ((HZ * 60) / 1000)
64
65 #define AK8975_POWERDOWN_WAIT_TIME      100
66
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
72
73 #define AK8975_DEV_ID           0x48
74
75 struct ak8975_data {
76         s16 x;
77         s16 y;
78         s16 z;
79 };
80
81 struct ak8975_chip {
82         struct i2c_client *client;
83         struct device *dev;
84         struct work_struct work;
85         struct mutex lock;
86         struct input_polled_dev *ipdev;
87
88         struct completion comp;
89
90         int poll_interval;
91
92         struct ak8975_data data;
93 };
94
95 static int ak8975_write_reg(struct i2c_client *client, u8 *buffer, int length)
96 {
97         struct i2c_msg msg[] = {
98                 {
99                         .addr = client->addr,
100                         .flags = 0,
101                         .len = length,
102                         .buf = buffer,
103                 },
104         };
105         return i2c_transfer(client->adapter, msg, 1);
106 }
107
108 static int ak8975_read_reg(struct i2c_client *client, u8 *buffer, int length)
109 {
110         struct i2c_msg msg[] = {
111                 {
112                         .addr = client->addr,
113                         .flags = 0,
114                         .len = 1,
115                         .buf = buffer,
116                 }, {
117                         .addr = client->addr,
118                         .flags = I2C_M_RD,
119                         .len = length,
120                         .buf = buffer,
121                 },
122         };
123         return i2c_transfer(client->adapter, msg, 2);
124 }
125
126 static void ak8975_update_data(struct ak8975_chip *ac)
127 {
128         u8 buffer[AK8975_NUM_OF_SENSOR_DATA];
129
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);
137 }
138
139 static void ak8975_work(struct work_struct *work)
140 {
141         struct ak8975_chip *ac = container_of(work, struct ak8975_chip, work);
142         static int counter = -1;
143
144         if (++counter == 0) {
145                 enable_irq(ac->client->irq);
146                 return;
147         }
148
149         ak8975_update_data(ac);
150         complete(&ac->comp);
151
152         if (ac->ipdev) {
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);
159         }
160         enable_irq(ac->client->irq);
161 }
162
163 static irqreturn_t ak8975_irq(int irq, void *data)
164 {
165         struct ak8975_chip *ac = data;
166
167         if (!work_pending(&ac->work)) {
168                 disable_irq_nosync(irq);
169                 schedule_work(&ac->work);
170         } else
171                 dev_err(&ac->client->dev, "work_pending\n");
172
173         return IRQ_HANDLED;
174 }
175
176 static ssize_t ak8975_show_output(struct device *dev,
177                 struct device_attribute *attr, char *buf)
178 {
179         struct ak8975_chip *ac = dev_get_drvdata(dev);
180         u8 buffer[2];
181
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);
186
187         return sprintf(buf, "%d %d %d\n",
188                         ac->data.x, ac->data.y, ac->data.z);
189 }
190 static DEVICE_ATTR(output, S_IRUGO, ak8975_show_output, NULL);
191
192 static ssize_t ak8975_set_i2c_disable(struct device *dev,
193                         struct device_attribute *attr, const char *buf,
194                         size_t count)
195 {
196         struct ak8975_chip *ac = dev_get_drvdata(dev);
197         unsigned long val;
198         u8 buffer[AK8975_NUM_OF_I2CDIS_DATA];
199         int ret;
200
201         mutex_lock(&ac->lock);
202         buffer[0] = AK8975_I2CDIS;
203         ret = strict_strtoul(buf, 10, &val);
204         if (ret) {
205                 dev_err(dev, "fail: conversion %s to number\n", buf);
206                 mutex_unlock(&ac->lock);
207                 return count;
208         }
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);
212         return count;
213 }
214 static DEVICE_ATTR(i2c_disable, S_IWUSR, NULL, ak8975_set_i2c_disable);
215
216 static ssize_t ak8975_show_overflow(struct device *dev,
217                         struct device_attribute *attr, char *buf)
218 {
219         struct ak8975_chip *ac = dev_get_drvdata(dev);
220         u8 buffer;
221
222         buffer = AK8975_ST2;
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);
227 }
228 static DEVICE_ATTR(overflow, S_IRUGO, ak8975_show_overflow, NULL);
229
230 static ssize_t ak8975_show_data_error(struct device *dev,
231                         struct device_attribute *attr, char *buf)
232 {
233         struct ak8975_chip *ac = dev_get_drvdata(dev);
234         u8 buffer;
235
236         buffer = AK8975_ST2;
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);
241 }
242 static DEVICE_ATTR(data_error, S_IRUGO, ak8975_show_data_error, NULL);
243
244 static ssize_t ak8975_show_SA(struct device *dev,
245                         struct device_attribute *attr, char *buf)
246 {
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];
250
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);
255
256         buffer[0] = AK8975_ASAX;
257         ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_ASA_DATA);
258
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]);
264 }
265 static DEVICE_ATTR(SA, S_IRUGO, ak8975_show_SA, NULL);
266
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,
272         &dev_attr_SA.attr,
273         NULL
274 };
275
276 static const struct attribute_group ak8975_group = {
277         .attrs = ak8975_attributes,
278 };
279
280 static void ak8975_initialize(struct ak8975_chip *ac)
281 {
282         u8 buffer[AK8975_NUM_OF_CNTL_DATA];
283
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);
288
289         udelay(AK8975_POWERDOWN_WAIT_TIME);
290 }
291
292 #ifdef CONFIG_INPUT_POLLDEV
293 static void ak8975_ipdev_poll(struct input_polled_dev *dev)
294 {
295         struct ak8975_chip *ac = dev->private;
296         u8 buffer[AK8975_NUM_OF_CNTL_DATA];
297
298         mutex_lock(&ac->lock);
299
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);
304
305         mutex_unlock(&ac->lock);
306 }
307
308 static int ak8975_register_input_dev(struct ak8975_chip *ac)
309 {
310         struct i2c_client *client = ac->client;
311         struct input_polled_dev *ipdev;
312         int ret;
313
314         ac->ipdev = ipdev = input_allocate_polled_device();
315         if (!ipdev) {
316                 dev_err(&client->dev, "fail: allocate poll input device\n");
317                 ret = -ENOMEM;
318                 goto error_input;
319         }
320
321         ipdev->private = ac;
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);
329
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);
336
337         ret = input_register_polled_device(ipdev);
338         if (ret) {
339                 dev_err(&client->dev, "fail: register to poll input device\n");
340                 goto error_reg_input;
341         }
342
343         return 0;
344
345 error_reg_input:
346         input_free_polled_device(ipdev);
347 error_input:
348         return ret;
349 }
350
351 static void ak8975_unregister_input_dev(struct ak8975_chip *ac)
352 {
353         input_unregister_polled_device(ac->ipdev);
354         input_free_polled_device(ac->ipdev);
355 }
356 #else
357 static int ak8975_register_input_dev(struct ak8975_chip *ac)
358 {
359         return 0;
360 }
361
362 static void ak8975_unregister_input_dev(struct ak8975_chip *ac)
363 {
364 }
365 #endif
366
367 static int ak8975_probe(struct i2c_client *client,
368                         const struct i2c_device_id *id)
369 {
370         struct ak8975_chip *ac;
371         struct ak8975_platform_data *pdata;
372         int ret = -1;
373         u8 dev_id;
374
375         ac = kzalloc(sizeof(struct ak8975_chip), GFP_KERNEL);
376         if (!ac)
377                 return -ENOMEM;
378
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;
384         }
385
386         pdata = client->dev.platform_data;
387
388         ac->poll_interval = pdata->poll_interval;
389         ac->client = client;
390         i2c_set_clientdata(client, ac);
391
392         INIT_WORK(&ac->work, ak8975_work);
393         mutex_init(&ac->lock);
394         init_completion(&ac->comp);
395
396         if (client->irq > 0) {
397                 ret = request_irq(client->irq, ak8975_irq, IRQF_TRIGGER_RISING,
398                                   "ak8975 compass", ac);
399                 if (ret) {
400                         dev_err(&client->dev, "can't get IRQ %d, ret %d\n",
401                                 client->irq, ret);
402                         goto error_irq;
403                 }
404         }
405
406         ret = sysfs_create_group(&client->dev.kobj, &ak8975_group);
407         if (ret) {
408                 dev_err(&client->dev, "Creating ak8975 attribute group failed");
409                 goto error_device;
410         }
411
412         ret = ak8975_register_input_dev(ac);
413         if (ret) {
414                 dev_err(&client->dev, "Registering ak8975 input device failed");
415                 goto error_input;
416         }
417
418         ak8975_initialize(ac);
419
420         pm_runtime_set_active(&client->dev);
421
422         dev_info(&client->dev, "%s registered\n", id->name);
423         return 0;
424
425 error_input:
426         sysfs_remove_group(&client->dev.kobj, &ak8975_group);
427 error_device:
428         free_irq(client->irq, ac);
429 error_devid_detect:
430 error_irq:
431         kfree(ac);
432         return ret;
433 }
434
435 static int __devexit ak8975_remove(struct i2c_client *client)
436 {
437         struct ak8975_chip *ac = i2c_get_clientdata(client);
438
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);
443         kfree(ac);
444         return 0;
445 }
446
447 #ifdef CONFIG_PM
448 static int ak8975_suspend(struct device *dev)
449 {
450         struct i2c_client *client = to_i2c_client(dev);
451         u8 buffer[AK8975_NUM_OF_CNTL_DATA];
452
453         disable_irq_nosync(client->irq);
454
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);
459
460         return 0;
461 }
462
463 static int ak8975_resume(struct device *dev)
464 {
465         struct i2c_client *client = to_i2c_client(dev);
466         struct ak8975_chip *ac = i2c_get_clientdata(client);
467
468         ak8975_initialize(ac);
469
470         enable_irq(client->irq);
471
472         return 0;
473 }
474
475 static struct dev_pm_ops ak8975_dev_pm_ops = {
476         .suspend        = ak8975_suspend,
477         .resume         = ak8975_resume,
478 };
479
480 #define AK8975_DEV_PM_OPS       (&ak8975_dev_pm_ops)
481 #else
482 #define AK8975_DEV_PM_OPS       NULL
483 #endif
484
485 static const struct i2c_device_id ak8975_id[] = {
486         {"ak8975", 0},
487         {}
488 };
489
490 MODULE_DEVICE_TABLE(i2c, ak8975_id);
491
492 static struct i2c_driver ak8975_i2c_driver = {
493         .driver = {
494                    .name        = "ak8975",
495                    .pm          = AK8975_DEV_PM_OPS,
496         },
497         .probe = ak8975_probe,
498         .remove = __devexit_p(ak8975_remove),
499         .id_table = ak8975_id,
500 };
501
502 static int __init ak8975_init(void)
503 {
504         return i2c_add_driver(&ak8975_i2c_driver);
505 }
506
507 module_init(ak8975_init);
508
509 static void __exit ak8975_exit(void)
510 {
511         i2c_del_driver(&ak8975_i2c_driver);
512 }
513
514 module_exit(ak8975_exit);
515
516 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
517 MODULE_DESCRIPTION("AK8975 compass driver");
518 MODULE_LICENSE("GPL");