upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / staging / iio / magnetometer / 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/completion.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 #include <linux/ak8975.h>
27
28 #include "../iio.h"
29 #include "../sysfs.h"
30 #include "magnet.h"
31
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
48
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)
53
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)
62
63 #define AK8975_OUT_MAX                  (4095)
64 #define AK8975_OUT_MIN                  (-4096)
65
66 #define AK8975_MAX_MEASUREMENT_TIME     ((HZ * 60) / 1000)
67
68 #define AK8975_POWERDOWN_WAIT_TIME      100
69
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
75
76 #define AK8975_DEV_ID                   0x48
77
78 #define AK8975_X_MEASUREMENT            1
79 #define AK8975_Y_MEASUREMENT            2
80 #define AK8975_Z_MEASUREMENT            3
81 #define AK8975_XYZ_MEASUREMENT          4
82
83 struct ak8975_data {
84         s16 x;
85         s16 y;
86         s16 z;
87 };
88
89 struct ak8975_chip {
90         struct i2c_client       *client;
91         struct iio_dev          *indio_dev;
92         struct work_struct      work;
93         struct mutex            lock;
94
95         struct completion       comp;
96
97         struct ak8975_data      data;
98         int                     measurement_type;
99
100         u8                      sa[AK8975_NUM_OF_ASA_DATA];
101         u8                      dev_id;
102         u8                      dev_info;
103 };
104
105 static int ak8975_write_reg(struct i2c_client *client, u8 *buffer, int length)
106 {
107         struct i2c_msg msg[] = {
108                 {
109                         .addr = client->addr,
110                         .flags = 0,
111                         .len = length,
112                         .buf = buffer,
113                 },
114         };
115         return i2c_transfer(client->adapter, msg, 1);
116 }
117
118 static int ak8975_read_reg(struct i2c_client *client, u8 *buffer, int length)
119 {
120         struct i2c_msg msg[] = {
121                 {
122                         .addr = client->addr,
123                         .flags = 0,
124                         .len = 1,
125                         .buf = buffer,
126                 }, {
127                         .addr = client->addr,
128                         .flags = I2C_M_RD,
129                         .len = length,
130                         .buf = buffer,
131                 },
132         };
133         return i2c_transfer(client->adapter, msg, 2);
134 }
135
136 static void ak8975_update_data(struct ak8975_chip *ac)
137 {
138         u8 buffer[AK8975_NUM_OF_XYZ_SENSOR_DATA];
139
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];
145                 break;
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];
150                 break;
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];
155                 break;
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];
162                 break;
163         default:
164                 break;
165         }
166 }
167
168 static void ak8975_work(struct work_struct *work)
169 {
170         struct ak8975_chip *ac = container_of(work, struct ak8975_chip, work);
171         static int counter = -1;
172
173         if (++counter == 0) {
174                 enable_irq(ac->client->irq);
175                 return;
176         }
177
178         ak8975_update_data(ac);
179         complete(&ac->comp);
180
181         enable_irq(ac->client->irq);
182 }
183
184 static irqreturn_t ak8975_irq(int irq, void *data)
185 {
186         struct ak8975_chip *ac = data;
187
188         if (!work_pending(&ac->work)) {
189                 disable_irq_nosync(irq);
190                 schedule_work(&ac->work);
191         } else
192                 dev_err(&ac->client->dev, "work_pending\n");
193
194         return IRQ_HANDLED;
195 }
196
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)   \
200 {                                                                       \
201         struct iio_dev *indio_dev = dev_get_drvdata(dev);               \
202         struct ak8975_chip *ac = indio_dev->dev_data;                   \
203         unsigned long val;                                              \
204         int ret;                                                        \
205         u8 buffer[AK8975_NUM_OF_CNTL_DATA];                             \
206                                                                         \
207         if (!count)                                                     \
208                 return -EINVAL;                                         \
209                                                                         \
210         ret = strict_strtoul(buf, 10, &val);                            \
211         if (ret)                                                        \
212                 return -EINVAL;                                         \
213                                                                         \
214         buffer[0] = reg;                                                \
215         buffer[1] = val;                                                \
216                                                                         \
217         ret = ak8975_write_reg(ac->client, buffer,                      \
218                         AK8975_NUM_OF_CNTL_DATA);                       \
219                                                                         \
220         if (ret < 0)                                                    \
221                 return ret;                                             \
222                                                                         \
223         return count;                                                   \
224 }                                                                       \
225 static ssize_t ak8975_show_##name(struct device *dev,                   \
226                 struct device_attribute *attr, char *buf)               \
227 {                                                                       \
228         struct iio_dev *indio_dev = dev_get_drvdata(dev);               \
229         struct ak8975_chip *ac = indio_dev->dev_data;                   \
230         u8 buffer;                                                      \
231                                                                         \
232         buffer = reg;                                                   \
233         ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_BYTE_DATA);  \
234         return sprintf(buf, "%d\n", buffer);                            \
235 }                                                                       \
236 static DEVICE_ATTR(name, S_IRUGO | S_IWUSR,                             \
237                 ak8975_show_##name, ak8975_store_##name);
238
239 AK8975_INPUT(self_test_control, AK8975_ASTC);
240 AK8975_INPUT(control, AK8975_CNTL);
241
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)               \
245 {                                                                       \
246         struct iio_dev *indio_dev = dev_get_drvdata(dev);               \
247         struct ak8975_chip *ac = indio_dev->dev_data;                   \
248         u8 buffer[2];                                                   \
249                                                                         \
250         mutex_lock(&ac->lock);                                          \
251                                                                         \
252         buffer[0] = AK8975_CNTL;                                        \
253         buffer[1] = measurement_mode;                                   \
254                                                                         \
255         ac->measurement_type = type;                                    \
256                                                                         \
257         ak8975_write_reg(ac->client, buffer, AK8975_NUM_OF_CNTL_DATA);  \
258         wait_for_completion_timeout(&ac->comp,                          \
259                                 AK8975_MAX_MEASUREMENT_TIME);           \
260                                                                         \
261         mutex_unlock(&ac->lock);                                        \
262                                                                         \
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);                        \
266         else                                                            \
267                 return sprintf(buf, "%d\n", ac->data.field);            \
268 }                                                                       \
269 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_##name, NULL);
270
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);
281
282 #define AK8975_OUTPUT_REG(name, reg)                                    \
283 static ssize_t ak8975_show_##name(struct device *dev,                   \
284                         struct device_attribute *attr, char *buf)       \
285 {                                                                       \
286         struct iio_dev *indio_dev = dev_get_drvdata(dev);               \
287         struct ak8975_chip *ac = indio_dev->dev_data;                   \
288         u8 buffer;                                                      \
289                                                                         \
290         buffer = reg;                                                   \
291         ak8975_read_reg(ac->client, &buffer, AK8975_NUM_OF_BYTE_DATA);  \
292         return sprintf(buf, "%d\n", buffer);                            \
293 }                                                                       \
294 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_##name, NULL);
295
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);
305
306 static ssize_t ak8975_show_calibbias(struct device *dev,
307                         struct device_attribute *attr, char *buf)
308 {
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);
312
313         return sprintf(buf, "%d\n", ac->sa[iio_attr->address]);
314 }
315
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);
322
323 static ssize_t ak8975_show_name(struct device *dev,
324                         struct device_attribute *attr,
325                         char *buf)
326 {
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);
330 }
331 static DEVICE_ATTR(name, S_IRUGO, ak8975_show_name, NULL);
332
333 static ssize_t ak8975_show_dev_id(struct device *dev,
334                         struct device_attribute *attr,
335                         char *buf)
336 {
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);
340 }
341 static DEVICE_ATTR(dev_id, S_IRUGO, ak8975_show_dev_id, NULL);
342
343 static ssize_t ak8975_show_dev_info(struct device *dev,
344                         struct device_attribute *attr,
345                         char *buf)
346 {
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);
350 }
351 static DEVICE_ATTR(dev_info, S_IRUGO, ak8975_show_dev_info, NULL);
352
353 static struct attribute *ak8975_attributes[] = {
354         &dev_attr_name.attr,
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,
367         &dev_attr_hxl.attr,
368         &dev_attr_hxh.attr,
369         &dev_attr_hyl.attr,
370         &dev_attr_hyh.attr,
371         &dev_attr_hzl.attr,
372         &dev_attr_hzh.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,
376         NULL
377 };
378
379 static const struct attribute_group ak8975_group = {
380         .attrs = ak8975_attributes,
381 };
382
383 static void ak8975_initialize(struct ak8975_chip *ac)
384 {
385         u8 buffer_cntl[AK8975_NUM_OF_CNTL_DATA];
386         u8 buffer[AK8975_NUM_OF_ASA_DATA];
387
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);
392
393         buffer[0] = AK8975_ASAX;
394         ak8975_read_reg(ac->client, buffer, AK8975_NUM_OF_ASA_DATA);
395
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);
400
401         ac->sa[0] = buffer[0];
402         ac->sa[1] = buffer[1];
403         ac->sa[2] = buffer[2];
404
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];
409 }
410
411 static int ak8975_probe(struct i2c_client *client,
412                         const struct i2c_device_id *id)
413 {
414         struct ak8975_chip *ac;
415         struct ak8975_platform_data *pdata;
416         int ret = -1;
417         u8 dev_id;
418
419         ac = kzalloc(sizeof(struct ak8975_chip), GFP_KERNEL);
420         if (!ac)
421                 return -ENOMEM;
422
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;
428         }
429         ac->dev_id = dev_id;
430
431         pdata = client->dev.platform_data;
432
433         ac->client = client;
434         i2c_set_clientdata(client, ac);
435
436         INIT_WORK(&ac->work, ak8975_work);
437         mutex_init(&ac->lock);
438         init_completion(&ac->comp);
439
440         ac->indio_dev = iio_allocate_device();
441         if (!ac->indio_dev) {
442                 ret = -ENOMEM;
443                 goto error_allocate_iio_device;
444         }
445
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;
451
452         ret = iio_device_register(ac->indio_dev);
453         if (ret)
454                 goto error_register_iio;
455
456         if (client->irq > 0) {
457                 ret = request_irq(client->irq, ak8975_irq, IRQF_TRIGGER_RISING,
458                                   "ak8975 compass", ac);
459                 if (ret) {
460                         dev_err(&client->dev, "can't get IRQ %d, ret %d\n",
461                                 client->irq, ret);
462                         goto error_irq;
463                 }
464         }
465
466         ak8975_initialize(ac);
467
468         pm_runtime_set_active(&client->dev);
469
470         dev_info(&client->dev, "%s registered\n", id->name);
471         return 0;
472
473 error_irq:
474         iio_device_unregister(ac->indio_dev);
475 error_register_iio:
476         iio_free_device(ac->indio_dev);
477 error_allocate_iio_device:
478 error_devid_detect:
479         kfree(ac);
480         return ret;
481 }
482
483 static int __devexit ak8975_remove(struct i2c_client *client)
484 {
485         struct ak8975_chip *ac = i2c_get_clientdata(client);
486
487         wait_for_completion(&ac->comp);
488         sysfs_remove_group(&client->dev.kobj, &ak8975_group);
489         free_irq(client->irq, ac);
490         kfree(ac);
491
492         return 0;
493 }
494
495 #ifdef CONFIG_PM
496 static int ak8975_suspend(struct device *dev)
497 {
498         struct i2c_client *client = to_i2c_client(dev);
499         u8 buffer[AK8975_NUM_OF_CNTL_DATA];
500
501         disable_irq_nosync(client->irq);
502
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);
507
508         return 0;
509 }
510
511 static int ak8975_resume(struct device *dev)
512 {
513         struct i2c_client *client = to_i2c_client(dev);
514         struct ak8975_chip *ac = i2c_get_clientdata(client);
515
516         ak8975_initialize(ac);
517
518         enable_irq(client->irq);
519
520         return 0;
521 }
522
523 static struct dev_pm_ops ak8975_dev_pm_ops = {
524         .suspend        = ak8975_suspend,
525         .resume         = ak8975_resume,
526 };
527
528 #define AK8975_DEV_PM_OPS       (&ak8975_dev_pm_ops)
529 #else
530 #define AK8975_DEV_PM_OPS       NULL
531 #endif
532
533 static const struct i2c_device_id ak8975_id[] = {
534         {"ak8975", 0},
535         {}
536 };
537
538 MODULE_DEVICE_TABLE(i2c, ak8975_id);
539
540 static struct i2c_driver ak8975_i2c_driver = {
541         .driver = {
542                    .name        = "ak8975",
543                    .pm          = AK8975_DEV_PM_OPS,
544         },
545         .probe = ak8975_probe,
546         .remove = __devexit_p(ak8975_remove),
547         .id_table = ak8975_id,
548 };
549
550 static int __init ak8975_init(void)
551 {
552         return i2c_add_driver(&ak8975_i2c_driver);
553 }
554
555 module_init(ak8975_init);
556
557 static void __exit ak8975_exit(void)
558 {
559         i2c_del_driver(&ak8975_i2c_driver);
560 }
561
562 module_exit(ak8975_exit);
563
564 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
565 MODULE_DESCRIPTION("AK8975 compass driver");
566 MODULE_LICENSE("GPL");