2 * (C) Copyright 2013 Bosch Sensortec GmbH All Rights Reserved
4 * This software program is licensed subject to the GNU General Public License
5 * (GPL).Version 2,June 1991, available at http://www.fsf.org/copyleft/gpl.html
9 * @brief BMM050 Linux Driver
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/input.h>
18 #include <linux/workqueue.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/sensors_core.h>
24 #ifdef CONFIG_HAS_EARLYSUSPEND
25 #include <linux/earlysuspend.h>
30 #include <linux/bst_sensor_common.h>
36 #define SENSOR_NAME "bmm050"
38 #define SENSOR_CHIP_ID_BMM (0x32)
40 #define BMM_REG_NAME(name) BMM050_##name
41 #define BMM_VAL_NAME(name) BMM050_##name
42 #define BMM_CALL_API(name) bmm050_##name
44 #define BMM_I2C_WRITE_DELAY_TIME 1
46 #define BMM_DEFAULT_REPETITION_XY BMM_VAL_NAME(REGULAR_REPXY)
47 #define BMM_DEFAULT_REPETITION_Z BMM_VAL_NAME(REGULAR_REPZ)
48 #define BMM_DEFAULT_ODR BMM_VAL_NAME(REGULAR_DR)
50 #define BMM_MAX_RETRY_I2C_XFER (100)
51 #define BMM_MAX_RETRY_WAKEUP (5)
52 #define BMM_MAX_RETRY_WAIT_DRDY (100)
54 #define BMM_DELAY_MIN (1)
55 #define BMM_DELAY_DEFAULT (200)
57 #define MAG_VALUE_MAX (32767)
58 #define MAG_VALUE_MIN (-32768)
60 #define BYTES_PER_LINE (16)
62 #define BMM_SELF_TEST 1
63 #define BMM_ADV_TEST 2
65 #define BMM_OP_MODE_UNKNOWN (-1)
67 static struct device *bmm_device = NULL;
73 static const u8 odr_map[] = {10, 2, 6, 8, 15, 20, 25, 30};
74 static const struct op_mode_map op_mode_maps[] = {
75 {"normal", BMM_VAL_NAME(NORMAL_MODE)},
76 {"forced", BMM_VAL_NAME(FORCED_MODE)},
77 {"suspend", BMM_VAL_NAME(SUSPEND_MODE)},
78 {"sleep", BMM_VAL_NAME(SLEEP_MODE)},
82 struct bmm_client_data {
84 struct i2c_client *client;
85 struct input_dev *input;
86 struct delayed_work work;
88 #ifdef CONFIG_HAS_EARLYSUSPEND
89 struct early_suspend early_suspend_handler;
93 /* whether the system in suspend state */
96 struct bmm050_mdata_s32 value;
105 struct mutex mutex_power_mode;
107 /* controls not only reg, but also workqueue */
108 struct mutex mutex_op_mode;
109 struct mutex mutex_enable;
110 struct mutex mutex_odr;
111 struct mutex mutex_rept_xy;
112 struct mutex mutex_rept_z;
114 struct mutex mutex_value;
115 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
116 struct bosch_sensor_specific *bst_pd;
120 static struct i2c_client *bmm_client;
121 /* i2c operation for API */
122 static void bmm_delay(u32 msec);
123 static char bmm_i2c_read(struct i2c_client *client, u8 reg_addr,
125 static char bmm_i2c_write(struct i2c_client *client, u8 reg_addr,
128 static void bmm_dump_reg(struct i2c_client *client);
129 static int bmm_wakeup(struct i2c_client *client);
130 static int bmm_check_chip_id(struct i2c_client *client);
132 static int bmm_pre_suspend(struct i2c_client *client);
133 static int bmm_post_resume(struct i2c_client *client);
135 #ifdef CONFIG_HAS_EARLYSUSPEND
136 static void bmm_early_suspend(struct early_suspend *handler);
137 static void bmm_late_resume(struct early_suspend *handler);
140 static int bmm_restore_hw_cfg(struct i2c_client *client);
142 static void bmm_remap_sensor_data(struct bmm050_mdata_s32 *val,
143 struct bmm_client_data *client_data)
145 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
146 struct bosch_sensor_data bsd;
148 if (NULL == client_data->bst_pd)
155 bst_remap_sensor_data_dft_tab(&bsd,
156 client_data->bst_pd->place);
167 static int bmm_check_chip_id(struct i2c_client *client)
172 bmm_i2c_read(client, BMM_REG_NAME(CHIP_ID), &chip_id, 1);
173 PINFO("read chip id result: %#x", chip_id);
175 if ((chip_id & 0xff) != SENSOR_CHIP_ID_BMM)
181 static void bmm_delay(u32 msec)
186 static inline int bmm_get_forced_drdy_time(int rept_xy, int rept_z)
188 return (145 * rept_xy + 500 * rept_z + 980 + (1000 - 1)) / 1000;
192 static void bmm_dump_reg(struct i2c_client *client)
197 u8 dbg_buf_str[64 * 3 + 1] = "";
199 for (i = 0; i < BYTES_PER_LINE; i++) {
201 sprintf(dbg_buf_str + i * 3, "%02x%c",
203 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
205 printk(KERN_DEBUG "%s\n", dbg_buf_str);
207 bmm_i2c_read(client, BMM_REG_NAME(CHIP_ID), dbg_buf, 64);
208 for (i = 0; i < 64; i++) {
209 sprintf(dbg_buf_str + i * 3, "%02x%c",
211 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
213 printk(KERN_DEBUG "%s\n", dbg_buf_str);
217 static int bmm_wakeup(struct i2c_client *client)
220 int try_times = BMM_MAX_RETRY_WAKEUP;
221 const u8 value = 0x01;
224 PINFO("waking up the chip...");
227 err = bmm_i2c_write(client,
228 BMM_REG_NAME(POWER_CNTL), (u8 *)&value, 1);
229 mdelay(BMM_I2C_WRITE_DELAY_TIME);
232 err = bmm_i2c_read(client, BMM_REG_NAME(POWER_CNTL), &dummy, 1);
239 PINFO("wake up result: %s, tried times: %d",
240 (try_times > 0) ? "succeed" : "fail",
241 BMM_MAX_RETRY_WAKEUP - try_times + 1);
243 err = (try_times > 0) ? 0 : -1;
248 /* i2c read routine for API*/
249 static char bmm_i2c_read(struct i2c_client *client, u8 reg_addr,
252 #if !defined BMM_USE_BASIC_I2C_FUNC
259 dummy = i2c_smbus_read_byte_data(client, reg_addr);
261 PERR("i2c bus read error");
264 *data = (u8)(dummy & 0xff);
266 dummy = i2c_master_send(client, (char *)®_addr, 1);
270 dummy = i2c_master_recv(client, (char *)data, 1);
281 struct i2c_msg msg[] = {
283 .addr = client->addr,
290 .addr = client->addr,
297 for (retry = 0; retry < BMM_MAX_RETRY_I2C_XFER; retry++) {
298 if (i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)) > 0)
301 mdelay(BMM_I2C_WRITE_DELAY_TIME);
304 if (BMM_MAX_RETRY_I2C_XFER <= retry) {
305 PERR("I2C xfer error");
313 /* i2c write routine for */
314 static char bmm_i2c_write(struct i2c_client *client, u8 reg_addr,
317 #if !defined BMM_USE_BASIC_I2C_FUNC
329 dummy = i2c_smbus_write_byte_data(client, reg_addr, *data);
331 buffer[0] = reg_addr;
333 dummy = i2c_master_send(client, (char *)buffer, 2);
338 PERR("error writing i2c bus");
347 struct i2c_msg msg[] = {
349 .addr = client->addr,
357 buffer[0] = reg_addr;
359 for (retry = 0; retry < BMM_MAX_RETRY_I2C_XFER; retry++) {
360 if (i2c_transfer(client->adapter, msg,
361 ARRAY_SIZE(msg)) > 0) {
364 mdelay(BMM_I2C_WRITE_DELAY_TIME);
367 if (BMM_MAX_RETRY_I2C_XFER <= retry) {
368 PERR("I2C xfer error");
379 static char bmm_i2c_read_wrapper(u8 dev_addr, u8 reg_addr, u8 *data, u8 len)
382 err = bmm_i2c_read(bmm_client, reg_addr, data, len);
387 static char bmm_i2c_write_wrapper(u8 dev_addr, u8 reg_addr, u8 *data, u8 len)
390 err = bmm_i2c_write(bmm_client, reg_addr, data, len);
394 /* this function exists for optimization of speed,
395 * because it is frequently called */
396 static inline int bmm_set_forced_mode(struct i2c_client *client)
401 const u8 value = 0x02;
402 err = bmm_i2c_write(client, BMM_REG_NAME(CONTROL), (u8 *)&value, 1);
407 static void bmm_work_func(struct work_struct *work)
409 struct bmm_client_data *client_data =
410 container_of((struct delayed_work *)work,
411 struct bmm_client_data, work);
412 struct i2c_client *client = client_data->client;
413 unsigned long delay =
414 msecs_to_jiffies(atomic_read(&client_data->delay));
416 mutex_lock(&client_data->mutex_value);
418 mutex_lock(&client_data->mutex_op_mode);
419 if (BMM_VAL_NAME(NORMAL_MODE) != client_data->op_mode)
420 bmm_set_forced_mode(client);
422 mutex_unlock(&client_data->mutex_op_mode);
424 BMM_CALL_API(read_mdataXYZ_s32)(&client_data->value);
425 bmm_remap_sensor_data(&client_data->value, client_data);
427 input_report_abs(client_data->input, ABS_X, client_data->value.datax);
428 input_report_abs(client_data->input, ABS_Y, client_data->value.datay);
429 input_report_abs(client_data->input, ABS_Z, client_data->value.dataz);
430 mutex_unlock(&client_data->mutex_value);
432 input_sync(client_data->input);
434 schedule_delayed_work(&client_data->work, delay);
438 static int bmm_set_odr(struct i2c_client *client, u8 odr)
442 err = BMM_CALL_API(set_datarate)(odr);
443 mdelay(BMM_I2C_WRITE_DELAY_TIME);
448 static int bmm_get_odr(struct i2c_client *client, u8 *podr)
453 err = BMM_CALL_API(get_datarate)(&value);
460 static ssize_t bmm_show_chip_id(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 return sprintf(buf, "%d\n", SENSOR_CHIP_ID_BMM);
466 static ssize_t bmm_show_op_mode(struct device *dev,
467 struct device_attribute *attr, char *buf)
470 struct input_dev *input = to_input_dev(dev);
471 struct bmm_client_data *client_data = input_get_drvdata(input);
475 mutex_lock(&client_data->mutex_power_mode);
476 BMM_CALL_API(get_powermode)(&power_mode);
478 mutex_lock(&client_data->mutex_op_mode);
479 BMM_CALL_API(get_functional_state)(&op_mode);
480 mutex_unlock(&client_data->mutex_op_mode);
482 op_mode = BMM_VAL_NAME(SUSPEND_MODE);
485 mutex_unlock(&client_data->mutex_power_mode);
487 PDEBUG("op_mode: %d", op_mode);
489 ret = sprintf(buf, "%d\n", op_mode);
495 static inline int bmm_get_op_mode_idx(u8 op_mode)
499 for (i = 0; i < ARRAY_SIZE(op_mode_maps); i++) {
500 if (op_mode_maps[i].op_mode == op_mode)
504 if (i < ARRAY_SIZE(op_mode_maps))
511 static int bmm_set_op_mode(struct bmm_client_data *client_data, int op_mode)
515 err = BMM_CALL_API(set_functional_state)(
518 if (BMM_VAL_NAME(SUSPEND_MODE) == op_mode)
519 atomic_set(&client_data->in_suspend, 1);
521 atomic_set(&client_data->in_suspend, 0);
526 static ssize_t bmm_store_op_mode(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf, size_t count)
532 struct input_dev *input = to_input_dev(dev);
533 struct bmm_client_data *client_data = input_get_drvdata(input);
534 struct i2c_client *client = client_data->client;
537 err = strict_strtoul(buf, 10, &op_mode);
541 mutex_lock(&client_data->mutex_power_mode);
543 i = bmm_get_op_mode_idx(op_mode);
546 mutex_lock(&client_data->mutex_op_mode);
547 if (op_mode != client_data->op_mode) {
548 if (BMM_VAL_NAME(FORCED_MODE) == op_mode) {
549 /* special treat of forced mode
550 * for optimization */
551 err = bmm_set_forced_mode(client);
553 err = bmm_set_op_mode(client_data, op_mode);
557 if (BMM_VAL_NAME(FORCED_MODE) == op_mode)
558 client_data->op_mode =
561 client_data->op_mode = op_mode;
564 mutex_unlock(&client_data->mutex_op_mode);
569 mutex_unlock(&client_data->mutex_power_mode);
577 static ssize_t bmm_show_odr(struct device *dev,
578 struct device_attribute *attr, char *buf)
580 unsigned char data = 0;
581 struct input_dev *input = to_input_dev(dev);
582 struct bmm_client_data *client_data = input_get_drvdata(input);
583 struct i2c_client *client = client_data->client;
587 mutex_lock(&client_data->mutex_power_mode);
588 BMM_CALL_API(get_powermode)(&power_mode);
590 mutex_lock(&client_data->mutex_odr);
591 err = bmm_get_odr(client, &data);
592 mutex_unlock(&client_data->mutex_odr);
596 mutex_unlock(&client_data->mutex_power_mode);
599 if (data < ARRAY_SIZE(odr_map))
600 err = sprintf(buf, "%d\n", odr_map[data]);
608 static ssize_t bmm_store_odr(struct device *dev,
609 struct device_attribute *attr,
610 const char *buf, size_t count)
615 struct input_dev *input = to_input_dev(dev);
616 struct bmm_client_data *client_data = input_get_drvdata(input);
617 struct i2c_client *client = client_data->client;
621 err = strict_strtoul(buf, 10, &tmp);
628 data = (unsigned char)tmp;
630 mutex_lock(&client_data->mutex_power_mode);
631 BMM_CALL_API(get_powermode)(&power_mode);
633 for (i = 0; i < ARRAY_SIZE(odr_map); i++) {
634 if (odr_map[i] == data)
638 if (i < ARRAY_SIZE(odr_map)) {
639 mutex_lock(&client_data->mutex_odr);
640 err = bmm_set_odr(client, i);
642 client_data->odr = i;
644 mutex_unlock(&client_data->mutex_odr);
652 mutex_unlock(&client_data->mutex_power_mode);
659 static ssize_t bmm_show_rept_xy(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 unsigned char data = 0;
663 struct input_dev *input = to_input_dev(dev);
664 struct bmm_client_data *client_data = input_get_drvdata(input);
668 mutex_lock(&client_data->mutex_power_mode);
669 BMM_CALL_API(get_powermode)(&power_mode);
670 printk(KERN_ALERT"%s : value of power_mode after get_powermode %u \n",__func__, power_mode);
673 mutex_lock(&client_data->mutex_rept_xy);
674 err = BMM_CALL_API(get_repetitions_XY)(&data);
675 printk(KERN_ALERT"%s : value of power_mode after get_repretitions_XY %u \n",__func__, data);
676 mutex_unlock(&client_data->mutex_rept_xy);
681 mutex_unlock(&client_data->mutex_power_mode);
686 return sprintf(buf, "%d\n", data);
689 static ssize_t bmm_store_rept_xy(struct device *dev,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
693 unsigned long tmp = 0;
694 struct input_dev *input = to_input_dev(dev);
695 struct bmm_client_data *client_data = input_get_drvdata(input);
700 err = strict_strtoul(buf, 10, &tmp);
707 data = (unsigned char)tmp;
709 mutex_lock(&client_data->mutex_power_mode);
710 BMM_CALL_API(get_powermode)(&power_mode);
711 printk(KERN_ALERT"%s : value of power_mode after get_powermode %u\n",__func__, power_mode);
714 mutex_lock(&client_data->mutex_rept_xy);
715 err = BMM_CALL_API(set_repetitions_XY)(data);
716 printk(KERN_ALERT "%s : value of power_mode after set_repretitions_XY %u \n",__func__,data);
719 mdelay(BMM_I2C_WRITE_DELAY_TIME);
720 client_data->rept_xy = data;
722 mutex_unlock(&client_data->mutex_rept_xy);
726 mutex_unlock(&client_data->mutex_power_mode);
734 static ssize_t bmm_show_rept_z(struct device *dev,
735 struct device_attribute *attr, char *buf)
737 unsigned char data = 0;
738 struct input_dev *input = to_input_dev(dev);
739 struct bmm_client_data *client_data = input_get_drvdata(input);
743 mutex_lock(&client_data->mutex_power_mode);
744 BMM_CALL_API(get_powermode)(&power_mode);
745 printk(KERN_ALERT"%s : value of power_mode after get_powermode %u\n",__func__, power_mode);
747 mutex_lock(&client_data->mutex_rept_z);
748 err = BMM_CALL_API(get_repetitions_Z)(&data);
749 printk(KERN_ALERT"%s : value of power_mode after get_repretitions_XY %u \n",__func__, data);
750 mutex_unlock(&client_data->mutex_rept_z);
755 mutex_unlock(&client_data->mutex_power_mode);
760 return sprintf(buf, "%d\n", data);
763 static ssize_t bmm_store_rept_z(struct device *dev,
764 struct device_attribute *attr,
765 const char *buf, size_t count)
767 unsigned long tmp = 0;
768 struct input_dev *input = to_input_dev(dev);
769 struct bmm_client_data *client_data = input_get_drvdata(input);
774 err = strict_strtoul(buf, 10, &tmp);
781 data = (unsigned char)tmp;
783 mutex_lock(&client_data->mutex_power_mode);
784 BMM_CALL_API(get_powermode)(&power_mode);
785 printk(KERN_ALERT" %s : value of power_mode after get_powermode %u\n",__func__, power_mode);
787 mutex_lock(&client_data->mutex_rept_z);
788 err = BMM_CALL_API(set_repetitions_Z)(data);
789 printk(KERN_ALERT"%s : value of power_mode after set_repretitions_XY %u \n",__func__, data);
791 mdelay(BMM_I2C_WRITE_DELAY_TIME);
792 client_data->rept_z = data;
794 mutex_unlock(&client_data->mutex_rept_z);
798 mutex_unlock(&client_data->mutex_power_mode);
807 static ssize_t bmm_show_value(struct device *dev,
808 struct device_attribute *attr, char *buf)
810 struct input_dev *input = to_input_dev(dev);
811 struct bmm_client_data *client_data = input_get_drvdata(input);
813 struct bmm050_mdata_s32 value = {0, 0, 0, 0, 0};
815 BMM_CALL_API(read_mdataXYZ_s32)(&value);
817 bmm_remap_sensor_data(&value, client_data);
818 client_data->value = value;
820 PERR("data not ready");
822 count = sprintf(buf, "%d %d %d\n",
823 client_data->value.datax,
824 client_data->value.datay,
825 client_data->value.dataz);
827 client_data->value.datax,
828 client_data->value.datay,
829 client_data->value.dataz);
835 static ssize_t bmm_show_value_raw(struct device *dev,
836 struct device_attribute *attr, char *buf)
838 struct bmm050_mdata value;
841 BMM_CALL_API(get_raw_xyz)(&value);
843 count = sprintf(buf, "%hd %hd %hd\n",
852 static ssize_t bmm_show_enable(struct device *dev,
853 struct device_attribute *attr, char *buf)
855 struct input_dev *input = to_input_dev(dev);
856 struct bmm_client_data *client_data = input_get_drvdata(input);
859 mutex_lock(&client_data->mutex_enable);
860 err = sprintf(buf, "%d\n", client_data->enable);
861 mutex_unlock(&client_data->mutex_enable);
865 static void bmm_input_destroy(struct bmm_client_data *client_data)
867 struct input_dev *dev = client_data->input;
869 input_unregister_device(dev);
870 input_free_device(dev);
873 static ssize_t bmm_store_enable(struct device *dev,
874 struct device_attribute *attr,
875 const char *buf, size_t count)
879 struct input_dev *input = to_input_dev(dev);
880 struct bmm_client_data *client_data = input_get_drvdata(input);
882 err = strict_strtoul(buf, 10, &data);
887 mutex_lock(&client_data->mutex_enable);
889 if (data != client_data->enable) {
892 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(NORMAL_MODE));
895 PERR(" %s : fail to init h/w of %s",__func__, SENSOR_NAME);
899 schedule_delayed_work(
901 msecs_to_jiffies(atomic_read(
902 &client_data->delay)));
905 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SUSPEND_MODE));
908 PERR(" %s : fail to init h/w of %s",__func__, SENSOR_NAME);
912 cancel_delayed_work_sync(&client_data->work);
915 client_data->enable = data;
917 mutex_unlock(&client_data->mutex_enable);
921 bmm_input_destroy(client_data);
926 static ssize_t bmm_show_delay(struct device *dev,
927 struct device_attribute *attr, char *buf)
929 struct input_dev *input = to_input_dev(dev);
930 struct bmm_client_data *client_data = input_get_drvdata(input);
932 return sprintf(buf, "%d\n", atomic_read(&client_data->delay));
936 static ssize_t bmm_store_delay(struct device *dev,
937 struct device_attribute *attr,
938 const char *buf, size_t count)
942 struct input_dev *input = to_input_dev(dev);
943 struct bmm_client_data *client_data = input_get_drvdata(input);
945 err = strict_strtoul(buf, 10, &data);
954 if (data < BMM_DELAY_MIN)
955 data = BMM_DELAY_MIN;
957 atomic_set(&client_data->delay, data);
962 static ssize_t bmm_show_test(struct device *dev,
963 struct device_attribute *attr, char *buf)
965 struct input_dev *input = to_input_dev(dev);
966 struct bmm_client_data *client_data = input_get_drvdata(input);
969 err = sprintf(buf, "%d\n", client_data->result_test);
973 static ssize_t bmm_store_test(struct device *dev,
974 struct device_attribute *attr,
975 const char *buf, size_t count)
979 struct input_dev *input = to_input_dev(dev);
980 struct bmm_client_data *client_data = input_get_drvdata(input);
981 struct i2c_client *client = client_data->client;
984 err = strict_strtoul(buf, 10, &data);
988 /* the following code assumes the work thread is not running */
989 if (BMM_SELF_TEST == data) {
991 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SLEEP_MODE));
993 err = BMM_CALL_API(set_selftest)(1);
995 err = BMM_CALL_API(get_self_test_XYZ)(&dummy);
996 client_data->result_test = dummy;
997 } else if (BMM_ADV_TEST == data) {
998 /* advanced self test */
999 err = BMM_CALL_API(perform_advanced_selftest)(
1000 &client_data->result_test);
1006 BMM_CALL_API(soft_reset)();
1007 mdelay(BMM_I2C_WRITE_DELAY_TIME);
1008 bmm_restore_hw_cfg(client);
1018 static ssize_t bmm_show_reg(struct device *dev,
1019 struct device_attribute *attr, char *buf)
1024 u8 dbg_buf_str[64 * 3 + 1] = "";
1025 struct input_dev *input = to_input_dev(dev);
1026 struct bmm_client_data *client_data = input_get_drvdata(input);
1027 struct i2c_client *client = client_data->client;
1029 for (i = 0; i < BYTES_PER_LINE; i++) {
1031 sprintf(dbg_buf_str + i * 3, "%02x%c",
1033 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
1035 memcpy(buf, dbg_buf_str, BYTES_PER_LINE * 3);
1037 for (i = 0; i < BYTES_PER_LINE * 3 - 1; i++)
1038 dbg_buf_str[i] = '-';
1040 dbg_buf_str[i] = '\n';
1041 memcpy(buf + BYTES_PER_LINE * 3, dbg_buf_str, BYTES_PER_LINE * 3);
1044 bmm_i2c_read(client, BMM_REG_NAME(CHIP_ID), dbg_buf, 64);
1045 for (i = 0; i < 64; i++) {
1046 sprintf(dbg_buf_str + i * 3, "%02x%c",
1048 (((i + 1) % BYTES_PER_LINE == 0) ? '\n' : ' '));
1050 memcpy(buf + BYTES_PER_LINE * 3 + BYTES_PER_LINE * 3,
1051 dbg_buf_str, 64 * 3);
1053 err = BYTES_PER_LINE * 3 + BYTES_PER_LINE * 3 + 64 * 3;
1058 static ssize_t bmm_show_place(struct device *dev,
1059 struct device_attribute *attr, char *buf)
1061 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
1062 struct input_dev *input = to_input_dev(dev);
1063 struct bmm_client_data *client_data = input_get_drvdata(input);
1065 int place = BOSCH_SENSOR_PLACE_UNKNOWN;
1067 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
1068 if (NULL != client_data->bst_pd)
1069 place = client_data->bst_pd->place;
1071 return sprintf(buf, "%d\n", place);
1075 static DEVICE_ATTR(chip_id, S_IRUGO,
1076 bmm_show_chip_id, NULL);
1077 static DEVICE_ATTR(op_mode, S_IRUGO|S_IWUSR,
1078 bmm_show_op_mode, bmm_store_op_mode);
1079 static DEVICE_ATTR(odr, S_IRUGO|S_IWUSR,
1080 bmm_show_odr, bmm_store_odr);
1081 static DEVICE_ATTR(rept_xy, S_IRUGO|S_IWUSR,
1082 bmm_show_rept_xy, bmm_store_rept_xy);
1083 static DEVICE_ATTR(rept_z, S_IRUGO|S_IWUSR,
1084 bmm_show_rept_z, bmm_store_rept_z);
1085 static DEVICE_ATTR(value, S_IRUGO,
1086 bmm_show_value, NULL);
1087 static DEVICE_ATTR(value_raw, S_IRUGO,
1088 bmm_show_value_raw, NULL);
1089 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR,
1090 bmm_show_enable, bmm_store_enable);
1091 static DEVICE_ATTR(delay, S_IRUGO|S_IWUSR,
1092 bmm_show_delay, bmm_store_delay);
1093 static DEVICE_ATTR(test, S_IRUGO|S_IWUSR,
1094 bmm_show_test, bmm_store_test);
1095 static DEVICE_ATTR(reg, S_IRUGO,
1096 bmm_show_reg, NULL);
1097 static DEVICE_ATTR(place, S_IRUGO,
1098 bmm_show_place, NULL);
1100 static struct attribute *bmm_attributes[] = {
1101 &dev_attr_chip_id.attr,
1102 &dev_attr_op_mode.attr,
1104 &dev_attr_rept_xy.attr,
1105 &dev_attr_rept_z.attr,
1106 &dev_attr_value.attr,
1107 &dev_attr_value_raw.attr,
1108 &dev_attr_enable.attr,
1109 &dev_attr_delay.attr,
1110 &dev_attr_test.attr,
1112 &dev_attr_place.attr,
1117 static struct attribute_group bmm_attribute_group = {
1118 .attrs = bmm_attributes
1122 static struct device_attribute *bmm050_attr[] = {
1124 &dev_attr_value_raw,
1129 static int bmm_input_init(struct bmm_client_data *client_data)
1131 struct input_dev *dev;
1134 dev = input_allocate_device();
1138 dev->name = SENSOR_NAME;
1139 dev->id.bustype = BUS_I2C;
1141 input_set_capability(dev, EV_ABS, ABS_MISC);
1142 input_set_abs_params(dev, ABS_X, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
1143 input_set_abs_params(dev, ABS_Y, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
1144 input_set_abs_params(dev, ABS_Z, MAG_VALUE_MIN, MAG_VALUE_MAX, 0, 0);
1145 input_set_drvdata(dev, client_data);
1147 err = input_register_device(dev);
1149 input_free_device(dev);
1152 client_data->input = dev;
1157 static int bmm_restore_hw_cfg(struct i2c_client *client)
1161 struct bmm_client_data *client_data =
1162 (struct bmm_client_data *)i2c_get_clientdata(client);
1165 mutex_lock(&client_data->mutex_op_mode);
1166 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SLEEP_MODE));
1168 if (bmm_get_op_mode_idx(client_data->op_mode) != -1)
1169 err = bmm_set_op_mode(client_data, client_data->op_mode);
1171 op_mode = client_data->op_mode;
1172 mutex_unlock(&client_data->mutex_op_mode);
1174 if (BMM_VAL_NAME(SUSPEND_MODE) == op_mode)
1177 PINFO("app did not close this sensor before suspend");
1179 mutex_lock(&client_data->mutex_odr);
1180 BMM_CALL_API(set_datarate)(client_data->odr);
1181 mdelay(BMM_I2C_WRITE_DELAY_TIME);
1182 mutex_unlock(&client_data->mutex_odr);
1184 mutex_lock(&client_data->mutex_rept_xy);
1185 err = bmm_i2c_write(client, BMM_REG_NAME(NO_REPETITIONS_XY),
1186 &client_data->rept_xy, 1);
1187 mdelay(BMM_I2C_WRITE_DELAY_TIME);
1188 err = bmm_i2c_read(client, BMM_REG_NAME(NO_REPETITIONS_XY), &value, 1);
1189 PINFO("BMM_NO_REPETITIONS_XY: %02x", value);
1190 mutex_unlock(&client_data->mutex_rept_xy);
1192 mutex_lock(&client_data->mutex_rept_z);
1193 err = bmm_i2c_write(client, BMM_REG_NAME(NO_REPETITIONS_Z),
1194 &client_data->rept_z, 1);
1195 mdelay(BMM_I2C_WRITE_DELAY_TIME);
1196 err = bmm_i2c_read(client, BMM_REG_NAME(NO_REPETITIONS_Z), &value, 1);
1197 PINFO("BMM_NO_REPETITIONS_Z: %02x", value);
1198 mutex_unlock(&client_data->mutex_rept_z);
1200 mutex_lock(&client_data->mutex_op_mode);
1201 if (BMM_OP_MODE_UNKNOWN == (int)client_data->op_mode) {
1202 bmm_set_forced_mode(client);
1203 PINFO("set forced mode after hw_restore");
1204 mdelay(bmm_get_forced_drdy_time(client_data->rept_xy,
1205 client_data->rept_z));
1207 mutex_unlock(&client_data->mutex_op_mode);
1210 PINFO("register dump after init");
1211 bmm_dump_reg(client);
1216 static int bmm_probe(struct i2c_client *client, const struct i2c_device_id *id)
1219 struct bmm_client_data *client_data = NULL;
1222 printk(KERN_ALERT"%s : function entrance\n",__func__);
1224 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1225 PERR("%s : i2c_check_functionality error!\n",__func__);
1227 goto exit_err_clean;
1230 if (NULL == bmm_client) {
1231 bmm_client = client;
1233 PERR("this driver does not support multiple clients");
1238 /* wake up the chip */
1239 dummy = bmm_wakeup(client);
1241 PERR("Cannot wake up %s, I2C xfer error", SENSOR_NAME);
1243 goto exit_err_clean;
1246 PINFO("register dump after waking up");
1247 bmm_dump_reg(client);
1249 err = bmm_check_chip_id(client);
1251 PNOTICE("Bosch Sensortec Device %s detected: %#x",
1252 SENSOR_NAME, client->addr);
1254 PERR("Bosch Sensortec Device not found, chip id mismatch");
1256 goto exit_err_clean;
1259 client_data = kzalloc(sizeof(struct bmm_client_data), GFP_KERNEL);
1260 if (NULL == client_data) {
1261 PERR("no memory available");
1263 goto exit_err_clean;
1266 i2c_set_clientdata(client, client_data);
1267 client_data->client = client;
1269 mutex_init(&client_data->mutex_power_mode);
1270 mutex_init(&client_data->mutex_op_mode);
1271 mutex_init(&client_data->mutex_enable);
1272 mutex_init(&client_data->mutex_odr);
1273 mutex_init(&client_data->mutex_rept_xy);
1274 mutex_init(&client_data->mutex_rept_z);
1275 mutex_init(&client_data->mutex_value);
1277 /* input device init */
1278 err = bmm_input_init(client_data);
1280 goto exit_err_clean;
1282 /* sysfs node creation */
1283 err = sysfs_create_group(&client_data->input->dev.kobj,
1284 &bmm_attribute_group);
1285 printk(KERN_ALERT"%s : sysfs for magnetic created \n",__func__);
1288 goto exit_err_sysfs;
1290 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
1292 printk(KERN_ALERT"%s Entering Plat Data bmm \n",__func__);
1293 if (NULL != client->dev.platform_data) {
1294 client_data->bst_pd = kzalloc(sizeof(*client_data->bst_pd),
1297 if (NULL != client_data->bst_pd) {
1298 memcpy(client_data->bst_pd, client->dev.platform_data,
1299 sizeof(*client_data->bst_pd));
1301 printk(KERN_ALERT" %s : platform data of bmm %s: place: %d, irq: %d",__func__,
1302 client_data->bst_pd->name,
1303 client_data->bst_pd->place,
1304 client_data->bst_pd->irq);
1310 /* workqueue init */
1311 INIT_DELAYED_WORK(&client_data->work, bmm_work_func);
1312 atomic_set(&client_data->delay, BMM_DELAY_DEFAULT);
1315 client_data->device.bus_read = bmm_i2c_read_wrapper;
1316 client_data->device.bus_write = bmm_i2c_write_wrapper;
1317 client_data->device.delay_msec = bmm_delay;
1319 BMM_CALL_API(init)(&client_data->device);
1321 bmm_dump_reg(client);
1323 PDEBUG(" %s : trimming_reg x1: %d y1: %d x2: %d y2: %d xy1: %d xy2: %d\n",__func__,
1324 client_data->device.dig_x1,
1325 client_data->device.dig_y1,
1326 client_data->device.dig_x2,
1327 client_data->device.dig_y2,
1328 client_data->device.dig_xy1,
1329 client_data->device.dig_xy2);
1331 PDEBUG(" %s : trimming_reg z1: %d z2: %d z3: %d z4: %d xyz1: %d\n",__func__,
1332 client_data->device.dig_z1,
1333 client_data->device.dig_z2,
1334 client_data->device.dig_z3,
1335 client_data->device.dig_z4,
1336 client_data->device.dig_xyz1);
1338 client_data->enable = 0;
1339 /* now it's power on which is considered as resuming from suspend */
1340 client_data->op_mode = BMM_VAL_NAME(SUSPEND_MODE);
1341 client_data->odr = BMM_DEFAULT_ODR;
1342 client_data->rept_xy = BMM_DEFAULT_REPETITION_XY;
1343 client_data->rept_z = BMM_DEFAULT_REPETITION_Z;
1347 err = bmm_restore_hw_cfg(client);
1350 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SUSPEND_MODE));
1352 PERR(" %s : fail to init h/w of %s",__func__, SENSOR_NAME);
1354 goto exit_err_sysfs;
1357 printk(KERN_ALERT"%s ... Removed SUSPEND_MODE \n",__func__);
1358 #ifdef CONFIG_HAS_EARLYSUSPEND
1359 client_data->early_suspend_handler.level =
1360 EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1361 client_data->early_suspend_handler.suspend = bmm_early_suspend;
1362 client_data->early_suspend_handler.resume = bmm_late_resume;
1363 register_early_suspend(&client_data->early_suspend_handler);
1366 err = sensors_register(&bmm_device,client_data,bmm050_attr, "magnetic_sensor");
1369 pr_err("%s: could not register" "magnetic_sensor device(%d).\n", __func__, err);
1370 goto exit_err_sysfs;
1373 PNOTICE(" %s : sensor %s probed successfully \n",__func__, SENSOR_NAME);
1375 PDEBUG(" %s : i2c_client: %p client_data: %p i2c_device: %p input: %p\n",__func__,
1376 client, client_data, &client->dev, client_data->input);
1382 bmm_input_destroy(client_data);
1386 if (client_data != NULL) {
1387 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
1388 if (NULL != client_data->bst_pd) {
1389 kfree(client_data->bst_pd);
1390 client_data->bst_pd = NULL;
1403 static int bmm_pre_suspend(struct i2c_client *client)
1406 struct bmm_client_data *client_data =
1407 (struct bmm_client_data *)i2c_get_clientdata(client);
1408 PDEBUG("function entrance");
1410 mutex_lock(&client_data->mutex_enable);
1411 if (client_data->enable) {
1412 cancel_delayed_work_sync(&client_data->work);
1413 PDEBUG("cancel work");
1415 mutex_unlock(&client_data->mutex_enable);
1420 static int bmm_post_resume(struct i2c_client *client)
1423 struct bmm_client_data *client_data =
1424 (struct bmm_client_data *)i2c_get_clientdata(client);
1426 mutex_lock(&client_data->mutex_enable);
1427 if (client_data->enable) {
1428 schedule_delayed_work(&client_data->work,
1430 atomic_read(&client_data->delay)));
1432 mutex_unlock(&client_data->mutex_enable);
1437 #ifdef CONFIG_HAS_EARLYSUSPEND
1438 static void bmm_early_suspend(struct early_suspend *handler)
1441 struct bmm_client_data *client_data =
1442 (struct bmm_client_data *)container_of(handler,
1443 struct bmm_client_data, early_suspend_handler);
1444 struct i2c_client *client = client_data->client;
1446 PDEBUG("function entrance");
1448 mutex_lock(&client_data->mutex_power_mode);
1449 BMM_CALL_API(get_powermode)(&power_mode);
1451 err = bmm_pre_suspend(client);
1452 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SUSPEND_MODE));
1454 mutex_unlock(&client_data->mutex_power_mode);
1458 static void bmm_late_resume(struct early_suspend *handler)
1461 struct bmm_client_data *client_data =
1462 (struct bmm_client_data *)container_of(handler,
1463 struct bmm_client_data, early_suspend_handler);
1464 struct i2c_client *client = client_data->client;
1465 PDEBUG("function entrance");
1467 mutex_lock(&client_data->mutex_power_mode);
1469 err = bmm_restore_hw_cfg(client);
1470 /* post resume operation */
1471 bmm_post_resume(client);
1473 mutex_unlock(&client_data->mutex_power_mode);
1476 static int bmm_suspend(struct i2c_client *client, pm_message_t mesg)
1479 struct bmm_client_data *client_data =
1480 (struct bmm_client_data *)i2c_get_clientdata(client);
1483 PDEBUG("function entrance");
1485 mutex_lock(&client_data->mutex_power_mode);
1486 BMM_CALL_API(get_powermode)(&power_mode);
1488 err = bmm_pre_suspend(client);
1489 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SUSPEND_MODE));
1491 mutex_unlock(&client_data->mutex_power_mode);
1496 static int bmm_resume(struct i2c_client *client)
1499 struct bmm_client_data *client_data =
1500 (struct bmm_client_data *)i2c_get_clientdata(client);
1502 PDEBUG("function entrance");
1504 mutex_lock(&client_data->mutex_power_mode);
1505 err = bmm_restore_hw_cfg(client);
1506 /* post resume operation */
1507 bmm_post_resume(client);
1509 mutex_unlock(&client_data->mutex_power_mode);
1515 static int bmm_remove(struct i2c_client *client)
1518 struct bmm_client_data *client_data =
1519 (struct bmm_client_data *)i2c_get_clientdata(client);
1521 if (NULL != client_data) {
1522 #ifdef CONFIG_HAS_EARLYSUSPEND
1523 unregister_early_suspend(&client_data->early_suspend_handler);
1526 mutex_lock(&client_data->mutex_op_mode);
1527 if (BMM_VAL_NAME(NORMAL_MODE) == client_data->op_mode) {
1528 cancel_delayed_work_sync(&client_data->work);
1529 PDEBUG("cancel work");
1531 mutex_unlock(&client_data->mutex_op_mode);
1533 err = bmm_set_op_mode(client_data, BMM_VAL_NAME(SUSPEND_MODE));
1534 mdelay(BMM_I2C_WRITE_DELAY_TIME);
1536 sensors_unregister(bmm_device, bmm050_attr);
1538 sysfs_remove_group(&client_data->input->dev.kobj,
1539 &bmm_attribute_group);
1540 bmm_input_destroy(client_data);
1542 #ifdef CONFIG_BMM_USE_PLATFORM_DATA
1543 if (NULL != client_data->bst_pd) {
1544 kfree(client_data->bst_pd);
1545 client_data->bst_pd = NULL;
1556 static const struct i2c_device_id bmm_id[] = {
1561 MODULE_DEVICE_TABLE(i2c, bmm_id);
1563 static struct i2c_driver bmm_driver = {
1565 .owner = THIS_MODULE,
1566 .name = SENSOR_NAME,
1568 .class = I2C_CLASS_HWMON,
1571 .remove = bmm_remove,
1572 #ifndef CONFIG_HAS_EARLYSUSPEND
1573 .suspend = bmm_suspend,
1574 .resume = bmm_resume,
1578 static int __init BMM_init(void)
1580 return i2c_add_driver(&bmm_driver);
1583 static void __exit BMM_exit(void)
1585 i2c_del_driver(&bmm_driver);
1588 MODULE_AUTHOR("Zhengguang.Guo <Zhengguang.Guo@bosch-sensortec.com>");
1589 MODULE_DESCRIPTION("driver for " SENSOR_NAME);
1590 MODULE_LICENSE("GPL");
1592 module_init(BMM_init);
1593 module_exit(BMM_exit);