]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/mxc_mma8451.c
f4407bc5beba9d1708c210a5d196ba6bf80a97ff
[karo-tx-linux.git] / drivers / hwmon / mxc_mma8451.c
1 /*
2  *  mma8451.c - Linux kernel modules for 3-Axis Orientation/Motion
3  *  Detection Sensor
4  *
5  *  Copyright (C) 2010-2013 Freescale Semiconductor, Inc. All Rights Reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/pm.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/hwmon.h>
34 #include <linux/input-polldev.h>
35 #include <linux/of.h>
36 #include <linux/regulator/consumer.h>
37
38 #define MMA8451_I2C_ADDR        0x1C
39 #define MMA8451_ID              0x1A
40 #define MMA8452_ID              0x2A
41 #define MMA8453_ID              0x3A
42
43 #define POLL_INTERVAL_MIN       1
44 #define POLL_INTERVAL_MAX       500
45 #define POLL_INTERVAL           100     /* msecs */
46 #define INPUT_FUZZ              32
47 #define INPUT_FLAT              32
48 #define MODE_CHANGE_DELAY_MS    100
49
50 #define MMA8451_STATUS_ZYXDR    0x08
51 #define MMA8451_BUF_SIZE        7
52 #define DEFAULT_POSITION        0
53
54 /* register enum for mma8451 registers */
55 enum {
56         MMA8451_STATUS = 0x00,
57         MMA8451_OUT_X_MSB,
58         MMA8451_OUT_X_LSB,
59         MMA8451_OUT_Y_MSB,
60         MMA8451_OUT_Y_LSB,
61         MMA8451_OUT_Z_MSB,
62         MMA8451_OUT_Z_LSB,
63
64         MMA8451_F_SETUP = 0x09,
65         MMA8451_TRIG_CFG,
66         MMA8451_SYSMOD,
67         MMA8451_INT_SOURCE,
68         MMA8451_WHO_AM_I,
69         MMA8451_XYZ_DATA_CFG,
70         MMA8451_HP_FILTER_CUTOFF,
71
72         MMA8451_PL_STATUS,
73         MMA8451_PL_CFG,
74         MMA8451_PL_COUNT,
75         MMA8451_PL_BF_ZCOMP,
76         MMA8451_P_L_THS_REG,
77
78         MMA8451_FF_MT_CFG,
79         MMA8451_FF_MT_SRC,
80         MMA8451_FF_MT_THS,
81         MMA8451_FF_MT_COUNT,
82
83         MMA8451_TRANSIENT_CFG = 0x1D,
84         MMA8451_TRANSIENT_SRC,
85         MMA8451_TRANSIENT_THS,
86         MMA8451_TRANSIENT_COUNT,
87
88         MMA8451_PULSE_CFG,
89         MMA8451_PULSE_SRC,
90         MMA8451_PULSE_THSX,
91         MMA8451_PULSE_THSY,
92         MMA8451_PULSE_THSZ,
93         MMA8451_PULSE_TMLT,
94         MMA8451_PULSE_LTCY,
95         MMA8451_PULSE_WIND,
96
97         MMA8451_ASLP_COUNT,
98         MMA8451_CTRL_REG1,
99         MMA8451_CTRL_REG2,
100         MMA8451_CTRL_REG3,
101         MMA8451_CTRL_REG4,
102         MMA8451_CTRL_REG5,
103
104         MMA8451_OFF_X,
105         MMA8451_OFF_Y,
106         MMA8451_OFF_Z,
107
108         MMA8451_REG_END,
109 };
110
111 /* The sensitivity is represented in counts/g. In 2g mode the
112 sensitivity is 1024 counts/g. In 4g mode the sensitivity is 512
113 counts/g and in 8g mode the sensitivity is 256 counts/g.
114  */
115 enum {
116         MODE_2G = 0,
117         MODE_4G,
118         MODE_8G,
119 };
120
121 enum {
122         MMA_STANDBY = 0,
123         MMA_ACTIVED,
124 };
125
126 /* mma8451 status */
127 struct mma8451_status {
128         u8 mode;
129         u8 ctl_reg1;
130         int active;
131         int position;
132 };
133
134 static struct mma8451_status mma_status;
135 static struct input_polled_dev *mma8451_idev;
136 static struct device *hwmon_dev;
137 static struct i2c_client *mma8451_i2c_client;
138
139 static int senstive_mode = MODE_2G;
140 static int ACCHAL[8][3][3] = {
141         { {0, -1, 0}, {1, 0, 0}, {0, 0, 1} },
142         { {-1, 0, 0}, {0, -1, 0}, {0, 0, 1} },
143         { {0, 1, 0}, {-1, 0, 0}, {0, 0, 1} },
144         { {1, 0, 0}, {0, 1, 0}, {0, 0, 1} },
145
146         { {0, -1, 0}, {-1, 0, 0}, {0, 0, -1} },
147         { {-1, 0, 0}, {0, 1, 0}, {0, 0, -1} },
148         { {0, 1, 0}, {1, 0, 0}, {0, 0, -1} },
149         { {1, 0, 0}, {0, -1, 0}, {0, 0, -1} },
150 };
151
152 static DEFINE_MUTEX(mma8451_lock);
153 static int mma8451_adjust_position(short *x, short *y, short *z)
154 {
155         short rawdata[3], data[3];
156         int i, j;
157         int position = mma_status.position;
158         if (position < 0 || position > 7)
159                 position = 0;
160         rawdata[0] = *x;
161         rawdata[1] = *y;
162         rawdata[2] = *z;
163         for (i = 0; i < 3; i++) {
164                 data[i] = 0;
165                 for (j = 0; j < 3; j++)
166                         data[i] += rawdata[j] * ACCHAL[position][i][j];
167         }
168         *x = data[0];
169         *y = data[1];
170         *z = data[2];
171         return 0;
172 }
173
174 static int mma8451_change_mode(struct i2c_client *client, int mode)
175 {
176         int result;
177
178         mma_status.ctl_reg1 = 0;
179         result = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1, 0);
180         if (result < 0)
181                 goto out;
182         mma_status.active = MMA_STANDBY;
183
184         result = i2c_smbus_write_byte_data(client, MMA8451_XYZ_DATA_CFG,
185                                            mode);
186         if (result < 0)
187                 goto out;
188         mdelay(MODE_CHANGE_DELAY_MS);
189         mma_status.mode = mode;
190
191         return 0;
192 out:
193         dev_err(&client->dev, "error when init mma8451:(%d)", result);
194         return result;
195 }
196
197 static int mma8451_read_data(short *x, short *y, short *z)
198 {
199         u8 tmp_data[MMA8451_BUF_SIZE];
200         int ret;
201
202         ret = i2c_smbus_read_i2c_block_data(mma8451_i2c_client,
203                                             MMA8451_OUT_X_MSB, 7, tmp_data);
204         if (ret < MMA8451_BUF_SIZE) {
205                 dev_err(&mma8451_i2c_client->dev, "i2c block read failed\n");
206                 return -EIO;
207         }
208
209         *x = ((tmp_data[0] << 8) & 0xff00) | tmp_data[1];
210         *y = ((tmp_data[2] << 8) & 0xff00) | tmp_data[3];
211         *z = ((tmp_data[4] << 8) & 0xff00) | tmp_data[5];
212         return 0;
213 }
214
215 static void report_abs(void)
216 {
217         short x, y, z;
218         int result;
219         int retry = 3;
220
221         mutex_lock(&mma8451_lock);
222         if (mma_status.active == MMA_STANDBY)
223                 goto out;
224         /* wait for the data ready */
225         do {
226                 result = i2c_smbus_read_byte_data(mma8451_i2c_client,
227                                                   MMA8451_STATUS);
228                 retry--;
229                 msleep(1);
230         } while (!(result & MMA8451_STATUS_ZYXDR) && retry > 0);
231         if (retry == 0)
232                 goto out;
233         if (mma8451_read_data(&x, &y, &z) != 0)
234                 goto out;
235         mma8451_adjust_position(&x, &y, &z);
236         input_report_abs(mma8451_idev->input, ABS_X, x);
237         input_report_abs(mma8451_idev->input, ABS_Y, y);
238         input_report_abs(mma8451_idev->input, ABS_Z, z);
239         input_sync(mma8451_idev->input);
240 out:
241         mutex_unlock(&mma8451_lock);
242 }
243
244 static void mma8451_dev_poll(struct input_polled_dev *dev)
245 {
246         report_abs();
247 }
248
249 static ssize_t mma8451_enable_show(struct device *dev,
250                                    struct device_attribute *attr, char *buf)
251 {
252         struct i2c_client *client;
253         u8 val;
254         int enable;
255
256         mutex_lock(&mma8451_lock);
257         client = mma8451_i2c_client;
258         val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
259         if ((val & 0x01) && mma_status.active == MMA_ACTIVED)
260                 enable = 1;
261         else
262                 enable = 0;
263         mutex_unlock(&mma8451_lock);
264         return sprintf(buf, "%d\n", enable);
265 }
266
267 static ssize_t mma8451_enable_store(struct device *dev,
268                                     struct device_attribute *attr,
269                                     const char *buf, size_t count)
270 {
271         struct i2c_client *client;
272         int ret;
273         unsigned long enable;
274         u8 val = 0;
275
276         ret = strict_strtoul(buf, 10, &enable);
277         if (ret) {
278                 dev_err(dev, "string transform error\n");
279                 return ret;
280         }
281
282         mutex_lock(&mma8451_lock);
283         client = mma8451_i2c_client;
284         enable = (enable > 0) ? 1 : 0;
285         if (enable && mma_status.active == MMA_STANDBY) {
286                 val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
287                 ret =
288                     i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
289                                               val | 0x01);
290                 if (!ret)
291                         mma_status.active = MMA_ACTIVED;
292
293         } else if (enable == 0 && mma_status.active == MMA_ACTIVED) {
294                 val = i2c_smbus_read_byte_data(client, MMA8451_CTRL_REG1);
295                 ret =
296                     i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
297                                               val & 0xFE);
298                 if (!ret)
299                         mma_status.active = MMA_STANDBY;
300
301         }
302         mutex_unlock(&mma8451_lock);
303         return count;
304 }
305
306 static ssize_t mma8451_position_show(struct device *dev,
307                                      struct device_attribute *attr, char *buf)
308 {
309         int position = 0;
310         mutex_lock(&mma8451_lock);
311         position = mma_status.position;
312         mutex_unlock(&mma8451_lock);
313         return sprintf(buf, "%d\n", position);
314 }
315
316 static ssize_t mma8451_position_store(struct device *dev,
317                                       struct device_attribute *attr,
318                                       const char *buf, size_t count)
319 {
320         unsigned long  position;
321         int ret;
322         ret = strict_strtoul(buf, 10, &position);
323         if (ret) {
324                 dev_err(dev, "string transform error\n");
325                 return ret;
326         }
327
328         mutex_lock(&mma8451_lock);
329         mma_status.position = (int)position;
330         mutex_unlock(&mma8451_lock);
331         return count;
332 }
333
334 static ssize_t mma8451_scalemode_show(struct device *dev,
335                                         struct device_attribute *attr,
336                                         char *buf)
337 {
338         int mode = 0;
339         mutex_lock(&mma8451_lock);
340         mode = (int)mma_status.mode;
341         mutex_unlock(&mma8451_lock);
342
343         return sprintf(buf, "%d\n", mode);
344 }
345
346 static ssize_t mma8451_scalemode_store(struct device *dev,
347                                         struct device_attribute *attr,
348                                         const char *buf, size_t count)
349 {
350         unsigned long  mode;
351         int ret, active_save;
352         struct i2c_client *client = mma8451_i2c_client;
353
354         ret = strict_strtoul(buf, 10, &mode);
355         if (ret) {
356                 dev_err(dev, "string transform error\n");
357                 goto out;
358         }
359
360         if (mode > MODE_8G) {
361                 dev_warn(dev, "not supported mode\n");
362                 ret = count;
363                 goto out;
364         }
365
366         mutex_lock(&mma8451_lock);
367         if (mode == mma_status.mode) {
368                 ret = count;
369                 goto out_unlock;
370         }
371
372         active_save = mma_status.active;
373         ret = mma8451_change_mode(client, mode);
374         if (ret)
375                 goto out_unlock;
376
377         if (active_save == MMA_ACTIVED) {
378                 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1, 1);
379
380                 if (ret)
381                         goto out_unlock;
382                 mma_status.active = active_save;
383         }
384
385 out_unlock:
386         mutex_unlock(&mma8451_lock);
387 out:
388         return ret;
389 }
390
391 static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO,
392                         mma8451_enable_show, mma8451_enable_store);
393 static DEVICE_ATTR(position, S_IWUSR | S_IRUGO,
394                         mma8451_position_show, mma8451_position_store);
395 static DEVICE_ATTR(scalemode, S_IWUSR | S_IRUGO,
396                         mma8451_scalemode_show, mma8451_scalemode_store);
397
398 static struct attribute *mma8451_attributes[] = {
399         &dev_attr_enable.attr,
400         &dev_attr_position.attr,
401         &dev_attr_scalemode.attr,
402         NULL
403 };
404
405 static const struct attribute_group mma8451_attr_group = {
406         .attrs = mma8451_attributes,
407 };
408
409 static int mma8451_probe(struct i2c_client *client,
410                                    const struct i2c_device_id *id)
411 {
412         int result, client_id;
413         struct input_dev *idev;
414         struct i2c_adapter *adapter;
415         u32 pos;
416         struct device_node *of_node = client->dev.of_node;
417         struct regulator *vdd, *vdd_io;
418
419         mma8451_i2c_client = client;
420
421         vdd = devm_regulator_get(&client->dev, "vdd");
422         if (!IS_ERR(vdd)) {
423                 result = regulator_enable(vdd);
424                 if (result) {
425                         dev_err(&client->dev, "vdd set voltage error\n");
426                         return result;
427                 }
428         }
429
430         vdd_io = devm_regulator_get(&client->dev, "vddio");
431         if (!IS_ERR(vdd_io)) {
432                 result = regulator_enable(vdd_io);
433                 if (result) {
434                         dev_err(&client->dev, "vddio set voltage error\n");
435                         return result;
436                 }
437         }
438
439         adapter = to_i2c_adapter(client->dev.parent);
440         result = i2c_check_functionality(adapter,
441                                          I2C_FUNC_SMBUS_BYTE |
442                                          I2C_FUNC_SMBUS_BYTE_DATA);
443         if (!result)
444                 goto err_out;
445
446         client_id = i2c_smbus_read_byte_data(client, MMA8451_WHO_AM_I);
447         if (client_id != MMA8451_ID && client_id != MMA8452_ID
448             && client_id != MMA8453_ID) {
449                 dev_err(&client->dev,
450                         "read chip ID 0x%x is not equal to 0x%x or 0x%x!\n",
451                         result, MMA8451_ID, MMA8452_ID);
452                 result = -EINVAL;
453                 goto err_out;
454         }
455
456         /* Initialize the MMA8451 chip */
457         result = mma8451_change_mode(client, senstive_mode);
458         if (result) {
459                 dev_err(&client->dev,
460                         "error when init mma8451 chip:(%d)\n", result);
461                 goto err_out;
462         }
463
464         hwmon_dev = hwmon_device_register(&client->dev);
465         if (!hwmon_dev) {
466                 result = -ENOMEM;
467                 dev_err(&client->dev, "error when register hwmon device\n");
468                 goto err_out;
469         }
470
471         mma8451_idev = input_allocate_polled_device();
472         if (!mma8451_idev) {
473                 result = -ENOMEM;
474                 dev_err(&client->dev, "alloc poll device failed!\n");
475                 goto err_alloc_poll_device;
476         }
477         mma8451_idev->poll = mma8451_dev_poll;
478         mma8451_idev->poll_interval = POLL_INTERVAL;
479         mma8451_idev->poll_interval_min = POLL_INTERVAL_MIN;
480         mma8451_idev->poll_interval_max = POLL_INTERVAL_MAX;
481         idev = mma8451_idev->input;
482         idev->name = "mma845x";
483         idev->id.bustype = BUS_I2C;
484         idev->evbit[0] = BIT_MASK(EV_ABS);
485
486         input_set_abs_params(idev, ABS_X, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
487         input_set_abs_params(idev, ABS_Y, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
488         input_set_abs_params(idev, ABS_Z, -8192, 8191, INPUT_FUZZ, INPUT_FLAT);
489
490         result = input_register_polled_device(mma8451_idev);
491         if (result) {
492                 dev_err(&client->dev, "register poll device failed!\n");
493                 goto err_register_polled_device;
494         }
495         result = sysfs_create_group(&idev->dev.kobj, &mma8451_attr_group);
496         if (result) {
497                 dev_err(&client->dev, "create device file failed!\n");
498                 result = -EINVAL;
499                 goto err_register_polled_device;
500         }
501
502         result = of_property_read_u32(of_node, "position", &pos);
503         if (result)
504                 pos = DEFAULT_POSITION;
505         mma_status.position = (int)pos;
506
507         return 0;
508 err_register_polled_device:
509         input_free_polled_device(mma8451_idev);
510 err_alloc_poll_device:
511         hwmon_device_unregister(&client->dev);
512 err_out:
513         return result;
514 }
515
516 static int mma8451_stop_chip(struct i2c_client *client)
517 {
518         int ret = 0;
519         if (mma_status.active == MMA_ACTIVED) {
520                 mma_status.ctl_reg1 = i2c_smbus_read_byte_data(client,
521                                                                MMA8451_CTRL_REG1);
522                 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
523                                                 mma_status.ctl_reg1 & 0xFE);
524         }
525         return ret;
526 }
527
528 static int mma8451_remove(struct i2c_client *client)
529 {
530         int ret;
531         ret = mma8451_stop_chip(client);
532         hwmon_device_unregister(hwmon_dev);
533
534         return ret;
535 }
536
537 #ifdef CONFIG_PM_SLEEP
538 static int mma8451_suspend(struct device *dev)
539 {
540         struct i2c_client *client = to_i2c_client(dev);
541
542         return mma8451_stop_chip(client);
543 }
544
545 static int mma8451_resume(struct device *dev)
546 {
547         int ret = 0;
548         struct i2c_client *client = to_i2c_client(dev);
549         if (mma_status.active == MMA_ACTIVED)
550                 ret = i2c_smbus_write_byte_data(client, MMA8451_CTRL_REG1,
551                                                 mma_status.ctl_reg1);
552         return ret;
553
554 }
555 #endif
556
557 static const struct i2c_device_id mma8451_id[] = {
558         {"mma8451", 0},
559 };
560
561 MODULE_DEVICE_TABLE(i2c, mma8451_id);
562
563 static SIMPLE_DEV_PM_OPS(mma8451_pm_ops, mma8451_suspend, mma8451_resume);
564 static struct i2c_driver mma8451_driver = {
565         .driver = {
566                    .name = "mma8451",
567                    .owner = THIS_MODULE,
568                    .pm = &mma8451_pm_ops,
569                    },
570         .probe = mma8451_probe,
571         .remove = mma8451_remove,
572         .id_table = mma8451_id,
573 };
574
575 static int __init mma8451_init(void)
576 {
577         /* register driver */
578         int res;
579
580         res = i2c_add_driver(&mma8451_driver);
581         if (res < 0) {
582                 printk(KERN_INFO "add mma8451 i2c driver failed\n");
583                 return -ENODEV;
584         }
585         return res;
586 }
587
588 static void __exit mma8451_exit(void)
589 {
590         i2c_del_driver(&mma8451_driver);
591 }
592
593 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
594 MODULE_DESCRIPTION("MMA8451 3-Axis Orientation/Motion Detection Sensor driver");
595 MODULE_LICENSE("GPL");
596
597 module_init(mma8451_init);
598 module_exit(mma8451_exit);