int bma020_acc_start(void) { int result; struct device *dev_t; bma020acc_t accels; /* only for test */ printk("%s \n",__func__); result = register_chrdev( BMA150_MAJOR, ACC_DEV_NAME, &acc_fops); if (result < 0) { return result; } acc_class = class_create (THIS_MODULE, "BMA-dev"); if (IS_ERR(acc_class)) { unregister_chrdev( BMA150_MAJOR, ACC_DEV_NAME); return PTR_ERR( acc_class ); } dev_t = device_create( acc_class, NULL, MKDEV(BMA150_MAJOR, 0), "%s", "accelerometer"); if (IS_ERR(dev_t)) { return PTR_ERR(dev_t); } result = i2c_acc_bma020_init(); if(result) { return result; } bma020_chip_init(); gprintk("[BMA020] read_xyz ==========================\n"); bma020_read_accel_xyz( &accels ); gprintk("[BMA020] x = %d / y = %d / z = %d\n", accels.x, accels.y, accels.z ); gprintk("[BMA020] ===================================\n"); /* only for test */ #if 0 printk( "before get xyz\n" ); mdelay(3000); while(1) { bma020_read_accel_xyz( &accels ); printk( "acc data x = %d / y = %d / z = %d\n", accels.x, accels.y, accels.z ); mdelay(100); } #endif #ifdef BMA020_PROC_FS create_proc_read_entry(DRIVER_PROC_ENTRY, 0, 0, bma020_proc_read, NULL); #endif //BMA020_PROC_FS bma020_set_mode(BMA020_MODE_SLEEP); gprintk("[BMA020] set_mode BMA020_MODE_SLEEP\n"); return 0; }
int bma020_acc_start(void) { int result,err; struct input_dev *input_dev; struct device *dev_t; bma020acc_t accels; result = register_chrdev( ACC_DEV_MAJOR, ACC_DEV_NAME, &acc_fops); if (result < 0) { return result; } acc_class = class_create (THIS_MODULE, ACC_DEV_NAME); if (IS_ERR(acc_class)) { unregister_chrdev( ACC_DEV_MAJOR, ACC_DEV_NAME); return PTR_ERR( acc_class ); } dev_t = device_create( acc_class, NULL, MKDEV(ACC_DEV_MAJOR, 0), "%s", ACC_DEV_NAME); if (IS_ERR(dev_t)) { return PTR_ERR(dev_t); } /*For testing*/ if (device_create_file(dev_t, &dev_attr_acc_file) < 0) printk("Failed to create device file %s \n", dev_attr_acc_file.attr.name); mutex_init(&bma020.power_lock); /* hrtimer settings. we poll for light values using a timer. */ hrtimer_init(&bma020.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); bma020.acc_poll_delay = ns_to_ktime(240 * NSEC_PER_MSEC); bma020.timer.function = bma_timer_func; /* the timer just fires off a work queue request. we need a thread to read the i2c (can be slow and blocking). */ bma020.wq = create_singlethread_workqueue("bma_wq"); if (!bma020.wq) { err = -ENOMEM; printk("%s: could not create workqueue\n", __func__); goto err_create_workqueue; } /* this is the thread function we run on the work queue */ INIT_WORK(&bma020.work_acc, bma_work_func_acc); /* allocate lightsensor-level input_device */ input_dev = input_allocate_device(); if (!input_dev) { printk("%s: could not allocate input device\n", __func__); err = -ENOMEM; goto err_input_allocate_device_light; } input_set_drvdata(input_dev, &bma020); input_dev->name = "accel"; set_bit(EV_ABS, input_dev->evbit); /* acceleration x-axis */ input_set_capability(input_dev, EV_ABS, ABS_X); input_set_abs_params(input_dev, ABS_X, -1024, 1024, 0, 0); /* acceleration y-axis */ input_set_capability(input_dev, EV_ABS, ABS_Y); input_set_abs_params(input_dev, ABS_Y, -1024, 1024, 0, 0); /* acceleration z-axis */ input_set_capability(input_dev, EV_ABS, ABS_Z); input_set_abs_params(input_dev, ABS_Z, -1024, 1024, 0, 0); printk("registering lightsensor-level input device\n"); err = input_register_device(input_dev); if (err < 0) { printk("%s: could not register input device\n", __func__); input_free_device(input_dev); goto err_input_register_device_light; } bma020.acc_input_dev = input_dev; err = sysfs_create_group(&input_dev->dev.kobj,&acc_attribute_group); if (err) { printk("Creating bma020 attribute group failed"); goto error_device; } ////////////////////////////////////////////////////////////////////////////// result = i2c_acc_bma020_init(); if(result) { return result; } bma020_chip_init(); gprintk("[BMA020] read_xyz ==========================\n"); bma020_read_accel_xyz( &accels ); gprintk("[BMA020] x = %d / y = %d / z = %d\n", accels.x, accels.y, accels.z ); gprintk("[BMA020] ===================================\n"); #ifdef BMA020_PROC_FS create_proc_read_entry(DRIVER_PROC_ENTRY, 0, 0, bma020_proc_read, NULL); #endif //BMA020_PROC_FS bma020_set_mode(BMA020_MODE_SLEEP); gprintk("[BMA020] set_mode BMA020_MODE_SLEEP\n"); return 0; error_device: sysfs_remove_group(&input_dev->dev.kobj, &acc_attribute_group); err_input_register_device_light: input_unregister_device(bma020.acc_input_dev); err_input_allocate_device_light: destroy_workqueue(bma020.wq); err_create_workqueue: mutex_destroy(&bma020.power_lock); exit: return err; }