blob: 830d2897afd6d4dda4f289775edb44e3998f3a16 [file] [log] [blame]
Colin Cross6659a742010-04-20 19:54:09 -07001/* drivers/misc/akm8975.c - akm8975 compass driver
2 *
3 * Copyright (C) 2007-2008 HTC Corporation.
4 * Author: Hou-Kun Chen <houkun.chen@gmail.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17/*
18 * Revised by AKM 2009/04/02
19 * Revised by Motorola 2010/05/27
20 *
21 */
22
23#include <linux/interrupt.h>
24#include <linux/i2c.h>
25#include <linux/slab.h>
26#include <linux/irq.h>
27#include <linux/miscdevice.h>
28#include <linux/gpio.h>
29#include <linux/uaccess.h>
30#include <linux/delay.h>
31#include <linux/input.h>
32#include <linux/workqueue.h>
33#include <linux/freezer.h>
34#include <linux/akm8975.h>
35#include <linux/earlysuspend.h>
36
37#define AK8975DRV_CALL_DBG 0
38#if AK8975DRV_CALL_DBG
39#define FUNCDBG(msg) pr_err("%s:%s\n", __func__, msg);
40#else
41#define FUNCDBG(msg)
42#endif
43
44#define AK8975DRV_DATA_DBG 0
45#define MAX_FAILURE_COUNT 10
46
47struct akm8975_data {
48 struct i2c_client *this_client;
49 struct akm8975_platform_data *pdata;
50 struct input_dev *input_dev;
51 struct work_struct work;
52 struct mutex flags_lock;
53#ifdef CONFIG_HAS_EARLYSUSPEND
54 struct early_suspend early_suspend;
55#endif
56};
57
58/*
59* Because misc devices can not carry a pointer from driver register to
60* open, we keep this global. This limits the driver to a single instance.
61*/
62struct akm8975_data *akmd_data;
63
64static DECLARE_WAIT_QUEUE_HEAD(open_wq);
65
66static atomic_t open_flag;
67
68static short m_flag;
69static short a_flag;
70static short t_flag;
71static short mv_flag;
72
73static short akmd_delay;
74
75static ssize_t akm8975_show(struct device *dev, struct device_attribute *attr,
76 char *buf)
77{
78 struct i2c_client *client = to_i2c_client(dev);
79 return sprintf(buf, "%u\n", i2c_smbus_read_byte_data(client,
80 AK8975_REG_CNTL));
81}
82static ssize_t akm8975_store(struct device *dev, struct device_attribute *attr,
83 const char *buf, size_t count)
84{
85 struct i2c_client *client = to_i2c_client(dev);
86 unsigned long val;
87 strict_strtoul(buf, 10, &val);
88 if (val > 0xff)
89 return -EINVAL;
90 i2c_smbus_write_byte_data(client, AK8975_REG_CNTL, val);
91 return count;
92}
93static DEVICE_ATTR(akm_ms1, S_IWUSR | S_IRUGO, akm8975_show, akm8975_store);
94
95static int akm8975_i2c_rxdata(struct akm8975_data *akm, char *buf, int length)
96{
97 struct i2c_msg msgs[] = {
98 {
99 .addr = akm->this_client->addr,
100 .flags = 0,
101 .len = 1,
102 .buf = buf,
103 },
104 {
105 .addr = akm->this_client->addr,
106 .flags = I2C_M_RD,
107 .len = length,
108 .buf = buf,
109 },
110 };
111
112 FUNCDBG("called");
113
114 if (i2c_transfer(akm->this_client->adapter, msgs, 2) < 0) {
115 pr_err("akm8975_i2c_rxdata: transfer error\n");
116 return EIO;
117 } else
118 return 0;
119}
120
121static int akm8975_i2c_txdata(struct akm8975_data *akm, char *buf, int length)
122{
123 struct i2c_msg msgs[] = {
124 {
125 .addr = akm->this_client->addr,
126 .flags = 0,
127 .len = length,
128 .buf = buf,
129 },
130 };
131
132 FUNCDBG("called");
133
134 if (i2c_transfer(akm->this_client->adapter, msgs, 1) < 0) {
135 pr_err("akm8975_i2c_txdata: transfer error\n");
136 return -EIO;
137 } else
138 return 0;
139}
140
141static void akm8975_ecs_report_value(struct akm8975_data *akm, short *rbuf)
142{
143 struct akm8975_data *data = i2c_get_clientdata(akm->this_client);
144
145 FUNCDBG("called");
146
147#if AK8975DRV_DATA_DBG
148 pr_info("akm8975_ecs_report_value: yaw = %d, pitch = %d, roll = %d\n",
149 rbuf[0], rbuf[1], rbuf[2]);
150 pr_info("tmp = %d, m_stat= %d, g_stat=%d\n", rbuf[3], rbuf[4], rbuf[5]);
151 pr_info("Acceleration: x = %d LSB, y = %d LSB, z = %d LSB\n",
152 rbuf[6], rbuf[7], rbuf[8]);
153 pr_info("Magnetic: x = %d LSB, y = %d LSB, z = %d LSB\n\n",
154 rbuf[9], rbuf[10], rbuf[11]);
155#endif
156 mutex_lock(&akm->flags_lock);
157 /* Report magnetic sensor information */
158 if (m_flag) {
159 input_report_abs(data->input_dev, ABS_RX, rbuf[0]);
160 input_report_abs(data->input_dev, ABS_RY, rbuf[1]);
161 input_report_abs(data->input_dev, ABS_RZ, rbuf[2]);
162 input_report_abs(data->input_dev, ABS_RUDDER, rbuf[4]);
163 }
164
165 /* Report acceleration sensor information */
166 if (a_flag) {
167 input_report_abs(data->input_dev, ABS_X, rbuf[6]);
168 input_report_abs(data->input_dev, ABS_Y, rbuf[7]);
169 input_report_abs(data->input_dev, ABS_Z, rbuf[8]);
170 input_report_abs(data->input_dev, ABS_WHEEL, rbuf[5]);
171 }
172
173 /* Report temperature information */
174 if (t_flag)
175 input_report_abs(data->input_dev, ABS_THROTTLE, rbuf[3]);
176
177 if (mv_flag) {
178 input_report_abs(data->input_dev, ABS_HAT0X, rbuf[9]);
179 input_report_abs(data->input_dev, ABS_HAT0Y, rbuf[10]);
180 input_report_abs(data->input_dev, ABS_BRAKE, rbuf[11]);
181 }
182 mutex_unlock(&akm->flags_lock);
183
184 input_sync(data->input_dev);
185}
186
187static void akm8975_ecs_close_done(struct akm8975_data *akm)
188{
189 FUNCDBG("called");
190 mutex_lock(&akm->flags_lock);
191 m_flag = 1;
192 a_flag = 1;
193 t_flag = 1;
194 mv_flag = 1;
195 mutex_unlock(&akm->flags_lock);
196}
197
198static int akm_aot_open(struct inode *inode, struct file *file)
199{
200 int ret = -1;
201
202 FUNCDBG("called");
203 if (atomic_cmpxchg(&open_flag, 0, 1) == 0) {
204 wake_up(&open_wq);
205 ret = 0;
206 }
207
208 ret = nonseekable_open(inode, file);
209 if (ret)
210 return ret;
211
212 file->private_data = akmd_data;
213
214 return ret;
215}
216
217static int akm_aot_release(struct inode *inode, struct file *file)
218{
219 FUNCDBG("called");
220 atomic_set(&open_flag, 0);
221 wake_up(&open_wq);
222 return 0;
223}
224
225static int akm_aot_ioctl(struct inode *inode, struct file *file,
226 unsigned int cmd, unsigned long arg)
227{
228 void __user *argp = (void __user *) arg;
229 short flag;
230 struct akm8975_data *akm = file->private_data;
231
232 FUNCDBG("called");
233
234 switch (cmd) {
235 case ECS_IOCTL_APP_SET_MFLAG:
236 case ECS_IOCTL_APP_SET_AFLAG:
237 case ECS_IOCTL_APP_SET_MVFLAG:
238 if (copy_from_user(&flag, argp, sizeof(flag)))
239 return -EFAULT;
240 if (flag < 0 || flag > 1)
241 return -EINVAL;
242 break;
243 case ECS_IOCTL_APP_SET_DELAY:
244 if (copy_from_user(&flag, argp, sizeof(flag)))
245 return -EFAULT;
246 break;
247 default:
248 break;
249 }
250
251 mutex_lock(&akm->flags_lock);
252 switch (cmd) {
253 case ECS_IOCTL_APP_SET_MFLAG:
254 m_flag = flag;
255 break;
256 case ECS_IOCTL_APP_GET_MFLAG:
257 flag = m_flag;
258 break;
259 case ECS_IOCTL_APP_SET_AFLAG:
260 a_flag = flag;
261 break;
262 case ECS_IOCTL_APP_GET_AFLAG:
263 flag = a_flag;
264 break;
265 case ECS_IOCTL_APP_SET_MVFLAG:
266 mv_flag = flag;
267 break;
268 case ECS_IOCTL_APP_GET_MVFLAG:
269 flag = mv_flag;
270 break;
271 case ECS_IOCTL_APP_SET_DELAY:
272 akmd_delay = flag;
273 break;
274 case ECS_IOCTL_APP_GET_DELAY:
275 flag = akmd_delay;
276 break;
277 default:
278 return -ENOTTY;
279 }
280 mutex_unlock(&akm->flags_lock);
281
282 switch (cmd) {
283 case ECS_IOCTL_APP_GET_MFLAG:
284 case ECS_IOCTL_APP_GET_AFLAG:
285 case ECS_IOCTL_APP_GET_MVFLAG:
286 case ECS_IOCTL_APP_GET_DELAY:
287 if (copy_to_user(argp, &flag, sizeof(flag)))
288 return -EFAULT;
289 break;
290 default:
291 break;
292 }
293
294 return 0;
295}
296
297static int akmd_open(struct inode *inode, struct file *file)
298{
299 int err = 0;
300
301 FUNCDBG("called");
302 err = nonseekable_open(inode, file);
303 if (err)
304 return err;
305
306 file->private_data = akmd_data;
307 return 0;
308}
309
310static int akmd_release(struct inode *inode, struct file *file)
311{
312 struct akm8975_data *akm = file->private_data;
313
314 FUNCDBG("called");
315 akm8975_ecs_close_done(akm);
316 return 0;
317}
318
319static int akmd_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
320 unsigned long arg)
321{
322 void __user *argp = (void __user *) arg;
323
324 char rwbuf[16];
325 int ret = -1;
326 int status;
327 short value[12];
328 short delay;
329 struct akm8975_data *akm = file->private_data;
330
331 FUNCDBG("called");
332
333 switch (cmd) {
334 case ECS_IOCTL_READ:
335 case ECS_IOCTL_WRITE:
336 if (copy_from_user(&rwbuf, argp, sizeof(rwbuf)))
337 return -EFAULT;
338 break;
339
340 case ECS_IOCTL_SET_YPR:
341 if (copy_from_user(&value, argp, sizeof(value)))
342 return -EFAULT;
343 break;
344
345 default:
346 break;
347 }
348
349 switch (cmd) {
350 case ECS_IOCTL_READ:
351 if (rwbuf[0] < 1)
352 return -EINVAL;
353
354 ret = akm8975_i2c_rxdata(akm, &rwbuf[1], rwbuf[0]);
355 if (ret < 0)
356 return ret;
357 break;
358
359 case ECS_IOCTL_WRITE:
360 if (rwbuf[0] < 2)
361 return -EINVAL;
362
363 ret = akm8975_i2c_txdata(akm, &rwbuf[1], rwbuf[0]);
364 if (ret < 0)
365 return ret;
366 break;
367 case ECS_IOCTL_SET_YPR:
368 akm8975_ecs_report_value(akm, value);
369 break;
370
371 case ECS_IOCTL_GET_OPEN_STATUS:
372 wait_event_interruptible(open_wq,
373 (atomic_read(&open_flag) != 0));
374 status = atomic_read(&open_flag);
375 break;
376 case ECS_IOCTL_GET_CLOSE_STATUS:
377 wait_event_interruptible(open_wq,
378 (atomic_read(&open_flag) == 0));
379 status = atomic_read(&open_flag);
380 break;
381
382 case ECS_IOCTL_GET_DELAY:
383 delay = akmd_delay;
384 break;
385
386 default:
387 FUNCDBG("Unknown cmd\n");
388 return -ENOTTY;
389 }
390
391 switch (cmd) {
392 case ECS_IOCTL_READ:
393 if (copy_to_user(argp, &rwbuf, sizeof(rwbuf)))
394 return -EFAULT;
395 break;
396 case ECS_IOCTL_GET_OPEN_STATUS:
397 case ECS_IOCTL_GET_CLOSE_STATUS:
398 if (copy_to_user(argp, &status, sizeof(status)))
399 return -EFAULT;
400 break;
401 case ECS_IOCTL_GET_DELAY:
402 if (copy_to_user(argp, &delay, sizeof(delay)))
403 return -EFAULT;
404 break;
405 default:
406 break;
407 }
408
409 return 0;
410}
411
412/* needed to clear the int. pin */
413static void akm_work_func(struct work_struct *work)
414{
415 struct akm8975_data *akm =
416 container_of(work, struct akm8975_data, work);
417
418 FUNCDBG("called");
419 enable_irq(akm->this_client->irq);
420}
421
422static irqreturn_t akm8975_interrupt(int irq, void *dev_id)
423{
424 struct akm8975_data *akm = dev_id;
425 FUNCDBG("called");
426
427 disable_irq_nosync(akm->this_client->irq);
428 schedule_work(&akm->work);
429 return IRQ_HANDLED;
430}
431
432static int akm8975_power_off(struct akm8975_data *akm)
433{
434#if AK8975DRV_CALL_DBG
435 pr_info("%s\n", __func__);
436#endif
437 if (akm->pdata->power_off)
438 akm->pdata->power_off();
439
440 return 0;
441}
442
443static int akm8975_power_on(struct akm8975_data *akm)
444{
445 int err;
446
447#if AK8975DRV_CALL_DBG
448 pr_info("%s\n", __func__);
449#endif
450 if (akm->pdata->power_on) {
451 err = akm->pdata->power_on();
452 if (err < 0)
453 return err;
454 }
455 return 0;
456}
457
458static int akm8975_suspend(struct i2c_client *client, pm_message_t mesg)
459{
460 struct akm8975_data *akm = i2c_get_clientdata(client);
461
462#if AK8975DRV_CALL_DBG
463 pr_info("%s\n", __func__);
464#endif
465 /* TO DO: might need more work after power mgmt
466 is enabled */
467 return akm8975_power_off(akm);
468}
469
470static int akm8975_resume(struct i2c_client *client)
471{
472 struct akm8975_data *akm = i2c_get_clientdata(client);
473
474#if AK8975DRV_CALL_DBG
475 pr_info("%s\n", __func__);
476#endif
477 /* TO DO: might need more work after power mgmt
478 is enabled */
479 return akm8975_power_on(akm);
480}
481
482#ifdef CONFIG_HAS_EARLYSUSPEND
483static void akm8975_early_suspend(struct early_suspend *handler)
484{
485 struct akm8975_data *akm;
486 akm = container_of(handler, struct akm8975_data, early_suspend);
487
488#if AK8975DRV_CALL_DBG
489 pr_info("%s\n", __func__);
490#endif
491 akm8975_suspend(akm->this_client, PMSG_SUSPEND);
492}
493
494static void akm8975_early_resume(struct early_suspend *handler)
495{
496 struct akm8975_data *akm;
497 akm = container_of(handler, struct akm8975_data, early_suspend);
498
499#if AK8975DRV_CALL_DBG
500 pr_info("%s\n", __func__);
501#endif
502 akm8975_resume(akm->this_client);
503}
504#endif
505
506
507static int akm8975_init_client(struct i2c_client *client)
508{
509 struct akm8975_data *data;
510 int ret;
511
512 data = i2c_get_clientdata(client);
513
514 ret = request_irq(client->irq, akm8975_interrupt, IRQF_TRIGGER_RISING,
515 "akm8975", data);
516
517 if (ret < 0) {
518 pr_err("akm8975_init_client: request irq failed\n");
519 goto err;
520 }
521
522 init_waitqueue_head(&open_wq);
523
524 mutex_lock(&data->flags_lock);
525 m_flag = 1;
526 a_flag = 1;
527 t_flag = 1;
528 mv_flag = 1;
529 mutex_unlock(&data->flags_lock);
530
531 return 0;
532err:
533 return ret;
534}
535
536static const struct file_operations akmd_fops = {
537 .owner = THIS_MODULE,
538 .open = akmd_open,
539 .release = akmd_release,
540 .ioctl = akmd_ioctl,
541};
542
543static const struct file_operations akm_aot_fops = {
544 .owner = THIS_MODULE,
545 .open = akm_aot_open,
546 .release = akm_aot_release,
547 .ioctl = akm_aot_ioctl,
548};
549
550static struct miscdevice akm_aot_device = {
551 .minor = MISC_DYNAMIC_MINOR,
552 .name = "akm8975_aot",
553 .fops = &akm_aot_fops,
554};
555
556static struct miscdevice akmd_device = {
557 .minor = MISC_DYNAMIC_MINOR,
558 .name = "akm8975_dev",
559 .fops = &akmd_fops,
560};
561
562int akm8975_probe(struct i2c_client *client,
563 const struct i2c_device_id *devid)
564{
565 struct akm8975_data *akm;
566 int err;
567 FUNCDBG("called");
568
569 if (client->dev.platform_data == NULL) {
570 dev_err(&client->dev, "platform data is NULL. exiting.\n");
571 err = -ENODEV;
572 goto exit_platform_data_null;
573 }
574
575 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
576 dev_err(&client->dev, "platform data is NULL. exiting.\n");
577 err = -ENODEV;
578 goto exit_check_functionality_failed;
579 }
580
581 akm = kzalloc(sizeof(struct akm8975_data), GFP_KERNEL);
582 if (!akm) {
583 dev_err(&client->dev,
584 "failed to allocate memory for module data\n");
585 err = -ENOMEM;
586 goto exit_alloc_data_failed;
587 }
588
589 akm->pdata = client->dev.platform_data;
590
591 mutex_init(&akm->flags_lock);
592 INIT_WORK(&akm->work, akm_work_func);
593 i2c_set_clientdata(client, akm);
594
595 err = akm8975_power_on(akm);
596 if (err < 0)
597 goto exit_power_on_failed;
598
599 akm8975_init_client(client);
600 akm->this_client = client;
601 akmd_data = akm;
602
603 akm->input_dev = input_allocate_device();
604 if (!akm->input_dev) {
605 err = -ENOMEM;
606 dev_err(&akm->this_client->dev,
607 "input device allocate failed\n");
608 goto exit_input_dev_alloc_failed;
609 }
610
611 set_bit(EV_ABS, akm->input_dev->evbit);
612
613 /* yaw */
614 input_set_abs_params(akm->input_dev, ABS_RX, 0, 23040, 0, 0);
615 /* pitch */
616 input_set_abs_params(akm->input_dev, ABS_RY, -11520, 11520, 0, 0);
617 /* roll */
618 input_set_abs_params(akm->input_dev, ABS_RZ, -5760, 5760, 0, 0);
619 /* x-axis acceleration */
620 input_set_abs_params(akm->input_dev, ABS_X, -5760, 5760, 0, 0);
621 /* y-axis acceleration */
622 input_set_abs_params(akm->input_dev, ABS_Y, -5760, 5760, 0, 0);
623 /* z-axis acceleration */
624 input_set_abs_params(akm->input_dev, ABS_Z, -5760, 5760, 0, 0);
625 /* temparature */
626 input_set_abs_params(akm->input_dev, ABS_THROTTLE, -30, 85, 0, 0);
627 /* status of magnetic sensor */
628 input_set_abs_params(akm->input_dev, ABS_RUDDER, 0, 3, 0, 0);
629 /* status of acceleration sensor */
630 input_set_abs_params(akm->input_dev, ABS_WHEEL, 0, 3, 0, 0);
631 /* x-axis of raw magnetic vector */
632 input_set_abs_params(akm->input_dev, ABS_HAT0X, -20480, 20479, 0, 0);
633 /* y-axis of raw magnetic vector */
634 input_set_abs_params(akm->input_dev, ABS_HAT0Y, -20480, 20479, 0, 0);
635 /* z-axis of raw magnetic vector */
636 input_set_abs_params(akm->input_dev, ABS_BRAKE, -20480, 20479, 0, 0);
637
638 akm->input_dev->name = "compass";
639
640 err = input_register_device(akm->input_dev);
641 if (err) {
642 pr_err("akm8975_probe: Unable to register input device: %s\n",
643 akm->input_dev->name);
644 goto exit_input_register_device_failed;
645 }
646
647 err = misc_register(&akmd_device);
648 if (err) {
649 pr_err("akm8975_probe: akmd_device register failed\n");
650 goto exit_misc_device_register_failed;
651 }
652
653 err = misc_register(&akm_aot_device);
654 if (err) {
655 pr_err("akm8975_probe: akm_aot_device register failed\n");
656 goto exit_misc_device_register_failed;
657 }
658
659 err = device_create_file(&client->dev, &dev_attr_akm_ms1);
660
661#ifdef CONFIG_HAS_EARLYSUSPEND
662 akm->early_suspend.suspend = akm8975_early_suspend;
663 akm->early_suspend.resume = akm8975_early_resume;
664 register_early_suspend(&akm->early_suspend);
665#endif
666 return 0;
667
668exit_misc_device_register_failed:
669exit_input_register_device_failed:
670 input_free_device(akm->input_dev);
671exit_input_dev_alloc_failed:
672 akm8975_power_off(akm);
673exit_power_on_failed:
674 kfree(akm);
675exit_alloc_data_failed:
676exit_check_functionality_failed:
677exit_platform_data_null:
678 return err;
679}
680
681static int __devexit akm8975_remove(struct i2c_client *client)
682{
683 struct akm8975_data *akm = i2c_get_clientdata(client);
684 FUNCDBG("called");
685 free_irq(client->irq, NULL);
686 input_unregister_device(akm->input_dev);
687 misc_deregister(&akmd_device);
688 misc_deregister(&akm_aot_device);
689 akm8975_power_off(akm);
690 kfree(akm);
691 return 0;
692}
693
694static const struct i2c_device_id akm8975_id[] = {
695 { "akm8975", 0 },
696 { }
697};
698
699MODULE_DEVICE_TABLE(i2c, akm8975_id);
700
701static struct i2c_driver akm8975_driver = {
702 .probe = akm8975_probe,
703 .remove = akm8975_remove,
704#ifndef CONFIG_HAS_EARLYSUSPEND
705 .resume = akm8975_resume,
706 .suspend = akm8975_suspend,
707#endif
708 .id_table = akm8975_id,
709 .driver = {
710 .name = "akm8975",
711 },
712};
713
714static int __init akm8975_init(void)
715{
716 pr_info("AK8975 compass driver: init\n");
717 FUNCDBG("AK8975 compass driver: init\n");
718 return i2c_add_driver(&akm8975_driver);
719}
720
721static void __exit akm8975_exit(void)
722{
723 FUNCDBG("AK8975 compass driver: exit\n");
724 i2c_del_driver(&akm8975_driver);
725}
726
727module_init(akm8975_init);
728module_exit(akm8975_exit);
729
730MODULE_AUTHOR("Hou-Kun Chen <hk_chen@htc.com>");
731MODULE_DESCRIPTION("AK8975 compass driver");
732MODULE_LICENSE("GPL");